Getting InvalidUseOfMatchersException even if all matcher are used for all arguments V1.9.5

16,871 views
Skip to first unread message

David Leonhartsberger

unread,
Oct 4, 2014, 11:22:23 AM10/4/14
to moc...@googlegroups.com
I get an InvalidUseOfMatchersException even if i use matchers for all arguments.
Please excuse me if I am posting a lot of code here but i think it is necessary to analyze the problem.

The exception is:

org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Invalid use of argument matchers!
0 matchers expected, 2 recorded:
-> at XXX.OngoingMessengerStubbing.sendRequest(OngoingMessengerStubbing.java:35)
-> at XXX.OngoingMessengerSendRequestStubbing.to(OngoingMessengerSendRequestStubbing.java:68)

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.

    at XXX.OngoingMessengerSendRequestStubbing.withCorrelationIdFromExecutionContext(OngoingMessengerSendRequestStubbing.java:56)
    at XXX.RadioPrivateCallTerminationStartedHandlerTest.handleEvent_forTerminationStartedEvent_requestIdFromSentReleaseMediaGraphRequestIsAddedAsInstanceKey(RadioPrivateCallTerminationStartedHandlerTest.java:73)


First this is my test method, that is executed.
Both context and messenger are mocks created by mockito.

*** RadioPrivateCallTerminationStartedHandlerTest.java ***

@Before
messenger
= mock(Messenger.class);

T message
= mock(Message.class);
when(message.getCorrelationId()).thenReturn(Randoms.alphanumeric("correlationId_"));
when(context.getMessage()).thenReturn(message);

@Test
   
public void handleEvent_forTerminationStartedEventWitOnlyRxConnection_aReleaseConnectionRequestIsSentForTheRxConnection() {
       
// given
       
String outgoingRequestId = whenMessenger(messenger) <-- this is line 73
               
.sendRequest(ReleaseConnectionRequest.class)
               
.to(MediaGraphServiceReference.PSI_ADDRESS)
               
.withCorrelationIdFromExecutionContext()
               
.thenReturnRandomRequestId();

 
// ...
}

protected OngoingMessengerStubbing whenMessenger(final Messenger messenger) {
   
return new OngoingMessengerStubbing(messenger, context);
}



*** OngoingMessengerStubbing.java ***

public OngoingMessengerSendRequestStubbing sendRequest(final Class<? extends Request> requestClazz) {
   
return new OngoingMessengerSendRequestStubbing(context, messenger, any(requestClazz)); <-- this is line 35
}



*** OngoingMessengerSendRequestStubbing.java ***

import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;

public class OngoingMessengerSendRequestStubbing {
   
private final ExecutionContext context;
   
private final Messenger messenger;
   
private final Request request;
   
private PsiAddress destination;
   
private String correlationId;
   
private boolean sendToSelf;

   
public OngoingMessengerSendRequestStubbing(
           
final ExecutionContext context,
           
final Messenger messenger,
           
final Request request) {

       
this.context = context;
       
this.messenger = messenger;
       
this.request = request;
   
}

   
public OngoingMessengerSendRequestStubbing withCorrelationId(final String theCorrelationId) {
       
this.correlationId = eq(theCorrelationId);
       
return this;
   
}

   
public OngoingMessengerSendRequestStubbing withCorrelationIdFromExecutionContext() {
       
if (context.getMessage() == null) {
           
throw new IllegalStateException("Message in context is null!"); <-- This is line 56
       
}

       
String id = context.getMessage().getCorrelationId();
       
this.correlationId = eq(id);
       
return this;
   
}

   
public OngoingMessengerSendRequestStubbing to(final PsiAddress theDestination) {
       
this.destination = eq(theDestination);  <-- this is line 68
       
return this;
   
}

   
public OngoingMessengerSendRequestStubbing toSelf() {
       
this.sendToSelf = true;
       
return this;
   
}

   
public String thenReturnRandomRequestId() {
       
String requestId = Randoms.alphanumeric("reqId_");
        setupMessengerToReturnRequestIdOnSend
(requestId);
       
return requestId;
   
}

   
private void setupMessengerToReturnRequestIdOnSend(final String requestId) {
       
if (!sendToSelf) {
           
Mockito.when(messenger.send(destination, correlationId, request)).thenReturn(requestId);
       
} else {
           
Mockito.when(messenger.sendToSelf(correlationId, request)).thenReturn(requestId);
       
}
   
}
}


David Wallace

unread,
Oct 4, 2014, 1:54:12 PM10/4/14
to moc...@googlegroups.com
Well, it looks like you're setting up a couple of matcher methods - any and eq - then calling context.getMessage().  You can't use matcher methods that way.  You need to set them up immediately before the stubbing call or verification call that uses them.  The reason is that matcher methods actually put an object into an internal Mockito structure, which the next stubbing or verification call then picks up.  If you try to use the wrong Mockito method when you've got those objects in the structure, you'll get the error that you're seeing.

For more detail on this, see Jeff Bowman's excellent article at http://stackoverflow.com/questions/22822512/how-do-mockito-matchers-work

It's really easiest to keep your matcher method calls inside the actual stubbing or verificaion call that needs them, as per all the examples you're ever likely to have seen.

--
You received this message because you are subscribed to the Google Groups "mockito" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mockito+u...@googlegroups.com.
To post to this group, send email to moc...@googlegroups.com.
Visit this group at http://groups.google.com/group/mockito.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages