-
Notifications
You must be signed in to change notification settings - Fork 72
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
Add @MockitoSettings(strictness = ...)
based on how Mockito is used, not based on version alone
#623
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some suggestions could not be made:
- src/main/java/org/openrewrite/java/testing/jmockit/JMockitBlockRewriter.java
- lines 112-113
It seems there are some confusing docs online, because there are many ways to use mockito. After our slack discussion I did some investigations. JUnit5In a project with dependencies: dependencies {
testImplementation(platform("org.junit:junit-bom:5.10.0"))
testImplementation("org.junit.jupiter:junit-jupiter")
testImplementation("org.mockito:mockito-core:<mockito_version>")
testImplementation("org.mockito:mockito-junit-jupiter:<mockito_version>")
} Given a java class @ExtendWith(MockitoExtension.class)
class TestingTest {
@Mock
private List<String> mockList;
@Test
void testing() {
// This would raise an UnnecessaryStubbingException as this mocked method is never called
when(mockList.add("one")).thenReturn(true); // this won't get called
System.out.println("Hello world!");
}
} This is throwing exception in following Mockito versions:
JUnit4For JUnit4 it seems there is a difference between the way they are ran In a project with dependencies dependencies {
testImplementation("junit:junit:4.12")
testImplementation("org.mockito:mockito-core:<mockito_version>")
} @RunWith(MockitoJUnitRunner.class)@RunWith(MockitoJUnitRunner.class)
public class TestingTest {
@Mock
private List<String> mockList;
@Test
public void testing() {
when(mockList.add("one")).thenReturn(true); // this won't get called
System.out.println("Hello world!");
}
} This is succesfull in Mockito versions:
This is throwing exception in following Mockito versions:
This exception can be silenced with @\Rule MockitoRulepublic class TestingTest {
@Rule
public MockitoRule rule = MockitoJUnit.rule();
@Mock
private List<String> mockList;
@Test
public void testing() {
when(mockList.add("one")).thenReturn(true); // this won't get called
System.out.println("Hello world!");
}
} This is succesfull in Mockito versions:
Mockito.mockpublic class TestingTest {
private List<String> mockList = Mockito.mock(List.class);
@Test
public void testing() {
when(mockList.add("one")).thenReturn(true); // this won't get called
System.out.println("Hello world!");
}
} This is succesfull in Mockito versions:
MockitoAnnotations.initMockspublic class TestingTest {
@Mock
private List<String> mockList;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
@Test
public void testing() {
when(mockList.add("one")).thenReturn(true); // this won't get called
System.out.println("Hello world!");
}
} This is succesfull in Mockito versions:
|
Oh wow, no wonder the docs are confusing. 😅 Thanks a lot for the detailed analysis and report! Wondering what to do here, as we do have recipes to migrate most of those onto JUnit 5, which would then start to fail on 2.17+ if we do not introduce Sounds like potentially our ScanningRecipe needs to keep track of what each test class was using (whenever we come from a pre-5 version), and add the annotation to the matching tests, if we want to be minimal in when we introduce that annotation. Would you agree @Jenson3210 ? |
I was at the same time writing a summary here. 😄 For For the For the I feel this means What do you think @timtebeek |
This reverts commit ffdcd85.
I agree based on the above that perhaps indeed adding the settings should be done at the same time as migrating from rules to extension. That seems to be the point where the behavior and defaults are changed, in a way that can not be reliably determined after those recipes have run based on the version number alone. I'd be open to even remove the RetainStrictnessWarn recipe; better to have one recipe reliably make those changes. |
I'll get started on some unit tests.
i think that mapping is the best |
@timtebeek, The code is ready and test are added. However, I am getting this issue with my 4 added tests. My class is all the time being prefixed with a space and I have no clue. Would you happen to know? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some suggestions could not be made:
- src/test/java/org/openrewrite/java/testing/mockito/MockitoJUnitToMockitoExtensionTest.java
- lines 19-20
@MockitoSettings(strictness = ...)
based on how Mockito is used, not based on version alone
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the detailed work here! Great to see this handled better in a way that wont break the tests at runtime. I'll follow up separately to remove the old RetainStrictnessWarn.
As Mockito only changed default strictness as of 4, repositories using 3 should also be patched.