[JAVA] [Mockito] 3.2.4 → 3.3.x, but I got an error (Iketenai)

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.

What kind of code was it?

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"));

What kind of error did you get?

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.

What's cool?

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.

Do I have to fix the code?

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! !!

Using the 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;

Use 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"));

Using 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);
// ...

JUnit 5: Using the 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.

Summary

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

[Mockito] 3.2.4 → 3.3.x, but I got an error (Iketenai)
I got an error when I ran rake routes.
I got an error "undefined method` create'" in RSpec
When I bcrypt with node + docker, I got an error
I got an error when using nextInt, nextLine and substring.
I got an error! * There is no interactive request template
I got an error and couldn't push! !! [error: failed to push some refs to]
I can deploy Heroku, but I get an error and can't open it
I built a CentOS 8 environment with Vagrant and tried to sync_folder, but I got an error, so I solved it.
I get an error when adding a dependency
I got an error when trying to install sbt to build a Scala development environment
[Docker] When I did docker-compose build, I got an error saying Can not read file. .. [Rails 6.0]
I can deploy to heroku, but I get an error and can't open it [Cause: MySQL]
On jetson nano, I tried pip install xxx.whl with Dockerfile but got an error (xxx.whl is not a supported wheel on this platform.)