After updating from Mockito 3.2.4 to 3.3.x (3.3.3) ... Is the code that sets the stub redundant? I got an error because of that.
It was such a code.
when(rs.getInt("column")).thenReturn(100);
assertEquals(Integer.valueOf(100), TYPE_HANDLER.getResult(rs, "column"));
when(rs.getInt("column")).thenReturn(0);
assertEquals(Integer.valueOf(0), TYPE_HANDLER.getResult(rs, "column"));
This is the error.
org.mockito.exceptions.misusing.UnnecessaryStubbingException:
Unnecessary stubbings detected.
Clean & maintainable test code requires zero unnecessary code.
There are 1 unnecessary stubbing (click to navigate to relevant line of code):
1. -> at org.apache.ibatis.type.YearTypeHandlerTest.shouldGetResultFromResultSetByName(YearTypeHandlerTest.java:44)
Please remove unnecessary stubbings or use 'lenient' strictness. More info: javadoc for UnnecessaryStubbingException class.
Apparently ... thenReturn
seems to be cool, and the error disappeared by doing the following.
when(rs.getInt("column")).thenReturn(100, 0); //Consolidate in one place! !!
assertEquals(Integer.valueOf(100), TYPE_HANDLER.getResult(rs, "column"));
assertEquals(Integer.valueOf(0), TYPE_HANDLER.getResult(rs, "column"));
Well ... it's like that.
If you don't have a clear intention, you should basically fix the code, but you can suppress this error by putting it in "lenient" mode as described in the error message. Here are some ways to deter it.
NOTE:
It's a deterrent that I've found in a quick search, so there may be other better ways! !!
lenient
attribute of @ Mock
When a mock object is created using annotation, it can be suppressed by specifying the attribute of annotation.
// @Mock
@Mock(lenient = true)
protected ResultSet rs;
MockSettings # lenient ()
If a mock object is created using the Mockito # mock
method, it can be suppressed by specifying the method option.
// ResultSet rs = mock(ResultSet.class);
ResultSet rs = mock(ResultSet.class, withSettings().lenient());
when(rs.getInt("column")).thenReturn(INSTANT.getValue());
assertEquals(INSTANT, TYPE_HANDLER.getResult(rs, "column"));
when(rs.getInt("column")).thenReturn(0);
assertEquals(Year.of(0), TYPE_HANDLER.getResult(rs, "column"));
Mockito.lenient ()
You can use Mockito.lenient ()
to suppress it, but it's better to fix it than to suppress it this way.
// when(rs.getInt("column")).thenReturn(100);
lenient().when(rs.getInt("column")).thenReturn(100);
// ...
strictness
attribute of @ MockitoSettings
If you are using JUnit 5 (MockitoExtension
), you can suppress it by specifying the attribute of the @MockitoSettings
annotation.
This setting seems to apply to the part that uses the Mockito # mock
method.
@MockitoSettings(strictness = Strictness.LENIENT) //Add this
@ExtendWith(MockitoExtension.class)
abstract class BaseTypeHandlerTest {
// ...
@Mock
protected PreparedStatement ps;
// ...
}
NOTE:
I won't mention it in this entry, but it seems that JUnit 4 has similar options.
Mickito is a very useful library for making unit test assumptions, but if you don't keep your code clean, it tends to look like "what are you doing?" So ... I'm grateful for this kind of check mechanism (sometimes I don't know how to fix it ...: sweat_smile :). However, the report contents of the library are not always correct, so it is a good idea to check the report contents and code, and consider using the "lenient" mode if necessary.
Recommended Posts