Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How do you handle InvalidUseOfMatchersException in Mockito


How do you handle InvalidUseOfMatchersException in Mockito


Handling the `InvalidUseOfMatchersException` in Mockito involves understanding the root cause of the issue and applying the correct solution. This exception typically occurs when you mix raw values with argument matchers in a method call that is being stubbed or verified. Here's a detailed guide on how to handle it:

Understanding the Exception

The `InvalidUseOfMatchersException` is thrown when Mockito detects that you are using a combination of raw values and argument matchers in a method call. For example, if a method takes two parameters and you use a raw value for one parameter and an argument matcher (like `any()`, `eq()`, etc.) for the other, Mockito will throw this exception.

Example of Incorrect Usage

java
// Incorrect usage: Mixing raw value with matcher
doNothing().when(cmd).dnsCheck(HOST, any(InetAddressFactory.class));

In this example, `HOST` is a raw value, and `any(InetAddressFactory.class)` is an argument matcher. This combination is not allowed.

Correcting the Exception

To fix the `InvalidUseOfMatchersException`, you must ensure that either all parameters are raw values or all parameters are argument matchers. Here are the correct ways to handle it:

Using All Argument Matchers

If you want to use argument matchers, you should use them for all parameters:

java
// Correct usage: Using all matchers
doNothing().when(cmd).dnsCheck(anyString(), any(InetAddressFactory.class));

In this corrected version, both parameters are specified using argument matchers (`anyString()` and `any(InetAddressFactory.class)`).

Using All Raw Values

Alternatively, you can use raw values for all parameters:

java
// Correct usage: Using all raw values
doNothing().when(cmd).dnsCheck(HOST, new InetAddressFactory());

Here, both parameters are raw values (`HOST` and a new instance of `InetAddressFactory`).

Additional Tips

- Avoid Mixing Matchers and Raw Values: Always ensure that you are consistent in using either all matchers or all raw values in a method call.
- Use `eq()` for Specific Values: If you need to match a specific value, use `eq()` to wrap it as a matcher. For example, `eq(HOST)` instead of just `HOST`.
- Check Mockito Version: Ensure you are using a compatible version of Mockito. Some features or behaviors might change between versions.

By following these guidelines, you can effectively handle and resolve the `InvalidUseOfMatchersException` in your Mockito tests.

Citations:
[1] https://community.lambdatest.com/t/how-to-fix-invaliduseofmatchersexception-in-mockito-unit-tests/31496
[2] https://github.com/mockito/mockito/issues/2785
[3] https://stackoverflow.com/questions/58769714/java-mockito-invaliduseofmatchersexception-when-mocking-method
[4] https://www.lambdatest.com/automation-testing-advisor/selenium/classes/org.mockito.invocation.Invocation
[5] https://www.lambdatest.com/automation-testing-advisor/selenium/methods/org.mockito.exceptions.misusing.WrongTypeOfReturnValue.InvalidUseOfMatchersException
[6] https://site.mockito.org/javadoc/current/index.html?org%2Fmockito%2Fexceptions%2Fmisusing%2FInvalidUseOfMatchersException.html
[7] https://javadoc.io/doc/org.mockito/mockito-core/2.0.48-beta/org/mockito/exceptions/misusing/InvalidUseOfMatchersException.html
[8] https://dzone.com/refcardz/mockito
[9] https://stackoverflow.com/questions/14845690/mockito-invaliduseofmatchersexception
[10] https://groups.google.com/g/mockito/c/g5Lo53dH-Cs
[11] https://groups.google.com/g/mockito/c/I-_kuURopFw
[12] https://coderanch.com/t/654045/engineering/org-mockito-exceptions-misusing-InvalidUseOfMatchersException