Mockito - 0 Matchers Expected, 1 Recorded (InvalidUseOfMatchersException)

invalid use of argument matchers 2 matchers expected, 1 recorded
mockito matchers
mockito when
mockito any interface
mockito when not working
mockito thenreturn(list)
for more info see javadoc for matchers class.
invaliduseofmatchersexception initmocks

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

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

Running this get's me:

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: Invalid use of argument matchers! 0 matchers expected, 1 recorded: at ...GridFileManagerTest.beforeClass(GridFileManagerTest.java:67)

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

For more info see javadoc for Matchers class.

If I were to do this though:

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

it no longer has that problem. This doesn't seem to accomplish what I want though - I want to return the value when the method is called with an object of type DBObject.

Thoughts?

I think your results are compatible with the result that would happen if dbCollection is not a Mockito-mock (or your method is static or final). That would mean that a matcher is being used where none can be used; hence the "0 matchers expected, 1 recorded".

Ambiguous Mockito, InvalidUseOfMatchersException: Invalid use of argument matchers! 0 matchers expected, 1 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. org.mockito.exceptions.misusing.InvalidUseOfMatchersException: Invalid use of argument matchers! 2 matchers expected, 1 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.

This same issue can be reproduced in Scala if you have default arguments. It may look like you're providing any() for every argument, but you should verify that the method definition doesn't have any default parameters which might be messing things up.

Getting InvalidUseOfMatchersException even if all matcher are used , org.mockito.exceptions.misusing.InvalidUseOfMatchersException: Invalid use of argument matchers! 0 matchers expected, 2 recorded: I get an InvalidUseOfMatchersException. Stepping into the Mockito code with the debugger, it looks like the problem is that MatchersBinder.validateMatchers() thinks the invocation is fooService.getMetaClass() rather than fooService.getMessage(). Is this a known issue?

Probably unrelated, but I encountered the same error when I spied a package private method. Changing it to public solved the issue for me.

Mockito - 0 Matchers Expected, 1 Recorded, Mockito - 0 Matchers Expected, 1 Recorded (InvalidUseOfMatchersException). Question. I'm trying to mock up some mongo classes so that I don't need a� java.lang.AssertionError: Expected: (an instance of java.lang.IllegalStateException and exception with message a string containing "") but: an instance of java.lang.IllegalStateException <org.mockito.exceptions.misusing.InvalidUseOfMatchersException: Invalid use of argument matchers! 2 matchers expected, 1 recorded: -> at MyTest.myTest(MyTest

Non-sensical "Invalid use of argument matchers!" error � Issue #193 , I am using org.mockito:mockito-core:1.9.0 with junit:junit:4.11. message InvalidUseOfMatchersException: 2 matchers expected, 1 recorded� Caused by: org.mockito.exceptions.misusing.InvalidUseOfMatchersException: Invalid use of argument matchers! 0 matchers expected, 1 recorded: -> at com.vinted.data.rx.api.ApiObserverTest.testOnThrowedApiError(ApiObserverTest.kt:174) This exception may occur if matchers are combined with raw values: //incorrect: someMethod(anyObject(), "raw

Can't create 'any' matcher for String or Java interface � Issue #32 , org.mockito.exceptions.misusing.InvalidUseOfMatchersException: Invalid use of argument matchers! 0 matchers expected, 1 recorded: -> at� I get the following exception: Exception in thread "main" org.mockito.exceptions.misusing.InvalidUseOfMatchersException: Invalid use of argument matchers! 0 matchers expected, 1 recorded: -> at org.Test.main(Test.java:8) 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

Mockito: InvalidUseOfMatchersException, Mockito: InvalidUseOfMatchersException. mockito eq 1 matchers expected, 2 recorded: mockito any 0 matchers expected, 1 recorded: you cannot use argument� org.mockito.exceptions.misusing.InvalidUseOfMatchersException: Invalid use of argument matchers! 4 matchers expected, 3 recorded Copy link Quote reply bohsen commented Oct 8, 2018 •

Comments
  • See [EDIT] part of first answer. Maybe you got same problem? stackoverflow.com/questions/10828843/…
  • so is DBCollection a Mockito mock object?
  • It's a mock(DBCollection.class)
  • But (assumning this is MongDB) it looks like DBCollection#find() is final, which has the same effect. You cannot mock final methods without using PowerMock.
  • It is indeed a final method, thanks for clarifying that for me!
  • I would if I could - I don't have the 15 reputation required.
  • Yes, also had issues with package private visible methods.