1

I have been getting writing unit tests for a method that invokes jdbcTemplate.query and returns some data. It doesn't seems to be working and throwing exception.

Here's the code.

@Test public void NewDealDaoGetClientOwnershipValuesTest() { List<OptionView> optionViews = new ArrayList<OptionView>(); optionViews.add(new OptionView("one", "two")); when(jdbcTemplate.query("<some sql query>", newDealDaoImpl.getResultSetExtractor(Mockito.anyString(), Mockito.anyString(), Mockito.anyString()))).thenReturn(optionViews); assertEquals(newDealDaoImpl.getClientOwnershipValues(), optionViews); } 

Error message

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: Invalid use of argument matchers! 2 matchers expected, 3 recorded. This exception may occur if matchers are combined with raw values: //incorrect: someMethod(anyObject(), "raw String"); When using matchers, all arguments have to be provided by matchers. For example: //correct: someMethod(anyObject(), eq("String by matcher")); 

Just for your information that method newDealDaoImpl.getResultSetExtractor takes 3 arguments<String, String, String>.

1 Answer 1

2

The problem is that you are using argument matchers:

Mockito.anyString() 

on an object that is not managed by Mockito (mock, spy etc.)

Try to pass an empty String or other random value to your:

newDealDaoImpl.getResultSetExtractor(...) 
Sign up to request clarification or add additional context in comments.

2 Comments

fixed code - Mockito.when(jdbcTemplate.query(Mockito.anyString(), (ResultSetExtractor<Object>) Mockito.anyObject())).thenReturn(optionViews);
Try to use any(ResultSetExtractor.class). anyObject() should be the last resort. Cheers

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.