user3123934
user3123934

Reputation: 1563

Why am I getting InvalidUseOfMatchersException here?

I am getting following error in my test case:

junit.framework.AssertionFailedError: Exception occured :
        org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
        Invalid use of argument matchers!
        2 matchers expected, 1 recorded:

This is my piece of code:

Mockito.when(mockHelloPeristenceImpl.retrieveHellorequest(Mockito.anyLong()))
              .thenReturn(Mockito.any(Hellorequest.class));

I tried all option suggested in the internet for this issue, nothing worked. What is wrong?

Upvotes: 2

Views: 86

Answers (1)

durron597
durron597

Reputation: 32323

You can't return a Matcher in the way you're doing it. You have to specify the actual object you're returning. Either do something like:

Mockito.when(mockHelloPeristenceImpl.retrieveHellorequest(Mockito.anyLong()))
       .thenReturn(Mockito.mock(Hellorequest.class));

Or, give it an answering strategy, e.g.

Mockito.when(mockHelloPeristenceImpl.retrieveHellorequest(Mockito.anyLong()))
       .then(Mockito.RETURNS_MOCKS);

As an aside, your code can be much shorter by using:

import static org.mockito.Mockito.*;

Then your test statement will be, simply:

when(mockHelloPeristenceImpl.retrieveHellorequest(anyLong()))
          .then(RETURNS_MOCKS);

Upvotes: 2

Related Questions