Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

The simple test case below is failing with an exception.

org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Invalid use of argument matchers! 3 matchers expected, 2 recorded:

I am not sure what is wrong

@Test
public void testGetStringTest(){

    final long testId = 1;
    String dlrBAC = null;
    NamedParameterJdbcTemplate jdbcTemplate = mock(NamedParameterJdbcTemplate.class);
    when(this.dao.getNamedParameterJdbcTemplate()).thenReturn(jdbcTemplate);
    when(jdbcTemplate.queryForObject(anyString(), any(SqlParameterSource.class), String.class
                        )).thenReturn("Test");
    dlrBAC =  dao.getStringTest(testId);
    assertNotNull(dlrBAC);

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
368 views
Welcome To Ask or Share your Answers For Others

1 Answer

Mockito requires you to either use only raw values or only matchers when stubbing a method call. The full exception (not posted by you here) surely explains everything.

Simple change the line:

when(jdbcTemplate.queryForObject(anyString(), any(SqlParameterSource.class), String.class
                        )).thenReturn("Test");

to

when(jdbcTemplate.queryForObject(anyString(), any(SqlParameterSource.class), eq(String.class)
                        )).thenReturn("Test");

and it should work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...