Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid use of argument matchers

Tags:

java

mockito

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);  } 
like image 876
Anwar Avatar asked Jun 28 '14 15:06

Anwar


People also ask

What is EQ Mockito?

Mockito Argument Matcher - eq() When we use argument matchers, then all the arguments should use matchers. If we want to use a specific value for an argument, then we can use eq() method. when(mockFoo. bool(eq("false"), anyInt(), any(Object. class))).


1 Answers

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.

like image 184
macias Avatar answered Sep 24 '22 02:09

macias