Anwar
Anwar

Reputation: 613

Invalid use of argument matchers

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

Upvotes: 61

Views: 172348

Answers (3)

Ashish
Ashish

Reputation: 1917

I was having Autowired annotation on the bean I was trying to mock. But, it should have been MockBean annotation.

Upvotes: 0

makasprzak
makasprzak

Reputation: 5220

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.

Upvotes: 120

Ahmad Alhaj-Karim
Ahmad Alhaj-Karim

Reputation: 194

For me I was using EasyMock and it was the wrong import. Make sure you have the correct imports of (anyString, any, anyInt).

Upvotes: 1

Related Questions