Mockito: InvalidUseOfMatchersException
The error message outlines the solution. The line
doNothing().when(cmd).dnsCheck(HOST, any(InetAddressFactory.class))
uses one raw value and one matcher, when it's required to use either all raw values or all matchers. A correct version might read
doNothing().when(cmd).dnsCheck(eq(HOST), any(InetAddressFactory.class))
Mockito Problems - InvalidUseOfMatchersException
This occurs because you are using any()
along with a real parameter VehicleCollection.class
of type Class<VehicleCollection>
.
Change it just like below, and you should be fine:
when(queryBus.handle(any(VehicleCollection.class), any(GetVehicle.class))).thenAnswer(null);
Mockito explains why: http://mockito.googlecode.com/svn/tags/1.7/javadoc/org/mockito/Matchers.html
If you are using argument matchers, all arguments have to be provided
by matchers.E.g: (example shows verification but the same applies to stubbing):
// Correct - eq() is also an argument matcher
verify(mock).someMethod(anyInt(), anyString(), eq("third argument"));
// Incorrect - exception will be thrown because third argument is given without argument matcher.
verify(mock).someMethod(anyInt(), anyString(), "third argument");
Mockito InvalidUseOfMatchersException despite using only matchers (and hint seems to mention wrong number of parameters)
Turns out it was because one of the variables in my 3rd argument, portcullisUtils
, was a mock object that I forgot to stub for getAdAccountRoleId(...)
.
I suppose the program can't really tell that of course I didn't mean to match against such an abstraction, and did something weird.
Will accept whoever's answer can explain why these particular error messages.
Java Mockito InvalidUseOfMatchersException when mocking method
In the "Act" part of your test, you are using argument matchers as parameters of the method under test:
List<FiscalClosing> result = fiscalClosingServiceImpl.getFiscalClosingListByNodeIdAndSiteId(anyString(), eq(siteId));
You can't use matchers like this, they should be used to define the behavior of the mock in a more generic way. When calling the method you should pass actual values, although they don't have to be valid depending on how you're mocking the rest of the method.
In your case it could be as simple as this:
List<FiscalClosing> result = fiscalClosingServiceImpl.getFiscalClosingListByNodeIdAndSiteId("nodeId", "siteId");
Then you might have to update your test to ensure that the correct site id is returned.
org.mockito.exceptions.misusing.InvalidUseOf
MatchersException: Invalid use of argument matchers
You called an argument matcher outside a call to when or verify, which is illegal.
See Argument matchers:
Matcher methods like
any()
,eq()
do not return matchers. Internally, they record a matcher on a stack and return a dummy value (usually null). This implementation is due to static type safety imposed by the java compiler. The consequence is that you cannot useany()
,eq()
methods outside of verified/stubbed method.
See also How do Mockito matchers work?
Related Topics
Jdbc Connection to Mssql Server in Windows Authentication Mode
Comparing Arrays in Junit Assertions, Concise Built-In Way
Passing Command Line Unicode Argument to Java Code
Java Sax Parser Split Calls to Characters()
How Can My Java Program Store Files Inside of Its .Jar File
Understanding Spring @Configuration Class
Java Swing: How to Implement a Login Screen Before Showing a Jframe
Java: Jprogressbar (Or Equivalent) in a Jtabbedpane Tab Title
Set Private Field Value with Reflection
What Is the Purpose of 'Class.Forname("My_Jdbc_Driver")'
Java Best Practice for Date Manipulation/Storage for Geographically Diverse Users
What Are the Differences Between "Generic" Types in C++ and Java
Why Main() Method Is Needed in Java Main Class