Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mockito: Recipe to replace initMocks with openMocks #512

Open
lucashan opened this issue Apr 25, 2024 · 1 comment
Open

Mockito: Recipe to replace initMocks with openMocks #512

lucashan opened this issue Apr 25, 2024 · 1 comment
Labels
mockito recipe Recipe request

Comments

@lucashan
Copy link

What problem are you trying to solve?

When upgrading Java applications and Mockito version, there is a MockitoAnnotiations API (initMocks) that is deprecated (see mockito-core 3.4.0 documentation for details).

What precondition(s) should be checked before applying this recipe?

The recommendation is to replace initMocks with openMocks(Object), which is equivalent to openMocks(testClass).close(). The close method should however only be called after completed usage of testClass. If using static-mocks or custom MockMakers, using this method might cause misbehavior of mocks injected into the test class.

Have you considered any alternatives or workarounds?

N/A

Any additional context

I'm not entirely sure if OpenRewrite has an existing utilities/mechanisms to simplify such complex refactorings, I wanted to surface this issue to check if there are any.

Are you interested in contributing this recipe to OpenRewrite?

N/A

@timtebeek timtebeek transferred this issue from openrewrite/rewrite-spring Apr 25, 2024
@timtebeek timtebeek moved this to Recipes Wanted in OpenRewrite Apr 25, 2024
@timtebeek timtebeek added the recipe Recipe request label Apr 25, 2024
@timtebeek
Copy link
Contributor

Thanks for the suggestion @lucashan ! We should be able to convert such cases as described with a new recipe. We already have a similar recipe test to bring folks from JUnit 4 and Mockito 1 to JUnit 5 and Mockito v3; seems only logical to automate the next migration test involving initMocks / openMocks as well.

Ideally we start with a unit test similar to this one, describing exactly what we should aim for with this recipe.

/**
* Replace org.mockito.MockitoAnnotations.Mock with org.mockito.Mock
*/
@DocumentExample
@Test
void replaceMockAnnotation() {
//language=java
rewriteRun(
spec -> spec.typeValidationOptions(TypeValidation.none()),
java(
"""
package org.openrewrite.java.testing.junit5;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import java.util.List;
import static org.mockito.Mockito.verify;
public class MockitoTests {
@Mock
List<String> mockedList;
@Before
public void initMocks() {
MockitoAnnotations.initMocks(this);
}
@Test
public void usingAnnotationBasedMock() {
mockedList.add("one");
mockedList.clear();
verify(mockedList).add("one");
verify(mockedList).clear();
}
}
""",
"""
package org.openrewrite.java.testing.junit5;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import java.util.List;
import static org.mockito.Mockito.verify;
public class MockitoTests {
@Mock
List<String> mockedList;
@BeforeEach
public void initMocks() {
MockitoAnnotations.initMocks(this);
}
@Test
public void usingAnnotationBasedMock() {
mockedList.add("one");
mockedList.clear();
verify(mockedList).add("one");
verify(mockedList).clear();
}
}
"""
)
);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mockito recipe Recipe request
Projects
Status: Recipes Wanted
Development

No branches or pull requests

2 participants