I have some async calls using CompletableFutures and my unit tests are failing. Most of the time they are not failing, but once in a while they may fail… I'm getting the error message that:
org.mockito.exceptions.misusing.PotentialStubbingProblem:
Strict stubbing argument mismatch. Please check:
- this invocation of 'queryForList' method:
namedParameterJdbcTemplate.queryForList(
null,
MapSqlParameterSource {id=0},
class java.lang.String
);
...
Typically, stubbing argument mismatch indicates user mistake when writing tests.
Mockito fails early so that you can debug potential problem easily.
However, there are legit scenarios when this exception generates false negative signal:
- stubbing the same method multiple times using 'given().will()' or 'when().then()' API
Please use 'will().given()' or 'doReturn().when()' API for stubbing.
- stubbed method is intentionally invoked with different arguments by code under test
Please use default or 'silent' JUnit Rule (equivalent of Strictness.LENIENT).
I've tried using doReturn().when() but it still gave the same error. Using Mockito.lenient() fixed it, but I feel using the Mockito.verify() method is the correct approach for this stubbing/async problem.
After adding the verification and the mock to return:
Mockito.verify(namedParameterJdbcTemplate, Mockito.timeout(100).times(1)).queryForList(Mockito.anyString(), Mockito.any(MapSqlParameterSource.class), Mockito.eq(String.class));
Mockito.verify(namedParameterJdbcTemplate, Mockito.timeout(100).times(1)).query(Mockito.anyString(), Mockito.any(MapSqlParameterSource.class), Mockito.any(BasicMapper.class));
Mockito.when(namedParameterJdbcTemplate.queryForList(Mockito.anyString(), Mockito.any(MapSqlParameterSource.class), Mockito.eq(String.class))).thenReturn(null);
Mockito.when(namedParameterJdbcTemplate.query(Mockito.anyString(), Mockito.any(MapSqlParameterSource.class), Mockito.any(BasicMapper.class))).thenReturn(offerEvents);
I got this error
Wanted but not invoked:
namedParameterJdbcTemplate.queryForList
...
Actually, there were zero interactions with this mock.
Whats the best way I can resolve this issue?