Mockito - 0 Expected Matches, 1 Logged (InvalidUseOfMatchersException)

I'm trying to mock some mongo classes, so I don't need a connection (pretty standard stuff), but the following code gives me problems:

when(dbCollection.find(isA(DBObject.class))).thenReturn(dbCursor); 

Running this me:

org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Invalid use of argument matches!
0 return matches, 1 record:
at ... GridFileManagerTest.beforeClass (GridFileManagerTest.java:67)

This exception may occur if the match is combined with raw values:
// false: someMethod (anyObject (), "raw String");

When using matches, all arguments must be provided by contests.
For instance:
// right:
someMethod (anyObject (), eq ("String by matcher"));

For more information, see javadoc for the Matchers class.

If I did this though:

 when(dbCollection.find(mock(DBObject.class))).thenReturn(dbCursor); 

he no longer has this problem. This is not like what I want - I want to return a value when the method is called with an object of type DBObject.

Thoughts?

+6
source share
1 answer

I think your results are compatible with the result that would happen if dbCollection not a Mockito-mock (or your method is static or final). This means that a connector is used where no one can be used; therefore expected "0 matches, 1 recorded."

+13
source

Source: https://habr.com/ru/post/920361/


All Articles