• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

org.mockito.exceptions.misusing.InvalidUseOfMatchersException:

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am getting following error on mockito
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:


I tried all option suggested in the internet for this issue, nothing worked.
Could some one help me on this?
Thanks
 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think the JSF forum is the place for this. Let me try moving it to somewhere more helpful.
 
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

shiva nath wrote:I am getting following error on mockito
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:


I tried all option suggested in the internet for this issue, nothing worked.
Could some one help me on this?
Thanks



Well it's been a few weeks since you posted this question, but better late than never I guess.

The problem here is that you've horribly confused Mockito by using a Matcher where it is not valid to use one. Matchers should be used in place of parameters to methods, but it doesn't make sense to use one in place of a return value. The thenReturn(...) method needs to be given a Hellorequest object (either a real one or a mock).

What I believe has happened here is that Mockito has seen your any(Hellorequest.class) matcher, but it is applying it to the next stubbing you're doing. You haven't shown enough code for me to be sure, but I assume you have another stubbing following that one that has 2 parameters but has not used Matchers. Mockito requires that whenever you make a stubbing you must either only supply real values or only supply Matchers. The reason for this is that Mockito is having to be very clever with its architecture to match up the Matchers you are using with the parameter you are meaning the Matcher to apply to. If you mix Matchers with real values it gets the Matchers and values out of order when it tries to match them to the actual parameters passed to the mock.

Now when you used a Matcher in the thenReturn() method Mockito thinks you are using that Matcher in the next stubbing you are performing (the next time you call when(...)). I assume that in that call you are not using Matchers, so it is telling you that you have misused the Matchers by mixing Matchers with real values.

I would also advise you to statically import the Mockito methods. The Mockito creators have gone to great lengths to produce an API that reads in near natural language, but that only works if you don't have to keep referencing the Mockito and Matchers class.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic