org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -
getting error org.mockito.exceptions.misusing.invaliduseofmatchersexception: invalid use of argument matchers! 1 matchers expected, 2 recorded. exception may occur if matchers combined raw values: //incorrect: somemethod(anyobject(), "raw string"); when using matchers, arguments have provided matchers. example: //correct: somemethod(anyobject(), eq("string matcher"));
at
when( getprogramservice .callservice(any(getprogramtype.class))) .thenreturn(jaxbresponse);
please explain error , possible resolution
the code posted looks fine; don't see problem it. code before or above causing exception. key here:
invalid use of argument matchers! 1 matchers expected, 2 recorded.
any
, used above, doesn't return "special kind of null" or "special kind of getprogramtype"; don't exist. instead, returns null , side effect puts matcher on stack. when checking matchers, mockito looks @ stack, , checks either empty (i.e. check equality arguments) or equal number of arguments in call you're checking (i.e. there matcher each argument).
what's happening here you're getting one more matcher callservice
method expects. see enough when developers mistakenly try save matcher in local variable:
string expectedstring = matchers.anystring(); // doesn't work // instead, adds matcher stack, // doesn't belong.
...or mock final method:
// assume getprogramservice.othermethod final. when(getprogramservice.othermethod(anystring())).thenreturn(123l); // calls getprogramservice.othermethod(null) , leaves // anystring matcher on stack without setting expectation // returns 123l.
to confirm problem, temporarily add statement before 1 posted:
mockito.validatemockitousage();
this artificially checks stack matchers, , throw exception on line. if does, check code above post. either way, adding surrounding code question debug it.
Comments
Post a Comment