I’m encountering an error: org.mockito.exceptions.misusing.InvalidUseOfMatchersException while writing unit tests using Mockito for a DNS check command line tool. In my runCommand method, I call dnsCheck with hostname and an InetAddressFactory instance. Here’s the setup:
public class Command {
void runCommand() {
dnsCheck(hostname, new InetAddressFactory());
// do other stuff after dnsCheck
}
void dnsCheck(String hostname, InetAddressFactory factory) {
// implementation
}
InetAddressFactory is used to mock the static InetAddress implementation:
public class InetAddressFactory {
public InetAddress getByName(String host) throws UnknownHostException {
return InetAddress.getByName(host);
}
}
In my unit test, I try to mock dnsCheck to test the code after this method:
@RunWith(MockitoJUnitRunner.class)
public class CmdTest {
@Test
void testPostDnsCheck() {
final Cmd cmd = spy(new Cmd());
// This line throws InvalidUseOfMatchersException
doNothing().when(cmd).dnsCheck(HOST, any(InetAddressFactory.class));
cmd.runCommand();
}
}
The error message I receive is:
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Invalid use of argument matchers!
2 matchers expected, 1 recorded.
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”));
How can I resolve this org.mockito.exceptions.misusing.InvalidUseOfMatchersException?
From what I’ve seen with Mockito, the error message is pointing to the fact that it’s not happy with the combination of a raw value (HOST) and a matcher (any(InetAddressFactory.class)
) in the same line. I’ve encountered this issue before, and the solution is to use either all raw values or all matchers.
In this case, the fix is pretty straightforward. You can use eq(HOST)
to make sure both arguments are being handled by matchers. Here’s the corrected line that I would use:
doNothing().when(cmd).dnsCheck(eq(HOST), any(InetAddressFactory.class));
This adjustment ensures consistency, and in my experience, it usually resolves the error right away. Let me know if this works for you!
Here are some steps to resolve the issue:
- Use MockitoJUnitRunner: Ensure you are using MockitoJUnitRunner in the @RunWith annotation, not SpringRunner. For example:
@RunWith(MockitoJUnitRunner.class)
-
Replace @MockBean with @Mock: In Mockito tests, use @Mock instead of @MockBean.
-
Use @InjectMocks Instead of @Autowired: The class under test should be annotated with @InjectMocks rather than @Autowired.
-
Initialize Private Members with Reflection: Use ReflectionTestUtils to initialize private members. For example:
ReflectionTestUtils.setField(manualRiskCasePipeline, “salesForceServiceClient”, salesForceServiceClient);
- Avoid Using isNull with thenReturn: Instead of using isNull in when().thenReturn(isNull), use null directly. For example:
when(someMethod()).thenReturn(null);
These adjustments should help resolve the InvalidUseOfMatchersException and other issues.
Despite using all the correct matchers, I was still encountering the issues:
"org.mockito.exceptions.misusing.InvalidUseOfMatchersException: Invalid use of argument matchers! 1 matcher expected, 3 recorded:"
I eventually realized that the method I was trying to mock was a static method of a class (e.g., Xyz.class). I had forgotten to include the following line:
PowerMockito.mockStatic(Xyz.class);
This might be the cause of the issue for others as well.