Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Can I combine multiple argument matchers in a single Mockito stubbing


Can I combine multiple argument matchers in a single Mockito stubbing


Yes, you can combine multiple argument matchers in a single Mockito stubbing. Mockito provides a variety of argument matchers that can be used together to define specific conditions under which a stubbed method should return a certain value. Here's how you can use them:

Using Multiple Argument Matchers

When using multiple argument matchers, it's crucial to use matchers for all arguments of the method you're stubbing. If you want to specify a specific value for an argument, you can use the `eq()` method. Here's an example:

java
when(mockFoo.bool(eq("false"), anyInt(), any(Object.class))).thenReturn(false);

In this example, `eq("false")` matches the string "false" exactly, `anyInt()` matches any integer, and `any(Object.class)` matches any object.

Combining Different Types of Matchers

You can combine different types of matchers to create complex conditions. For instance, you might use `any()` for arrays, `anyBoolean()` for boolean values, and `eq()` for exact matches:

java
when(mockFoo.bar(any(byte[].class), aryEq(new String[] { "A", "B" }), gt(10))).thenReturn(11);

Here, `any(byte[].class)` matches any byte array, `aryEq(new String[] { "A", "B" })` matches the specified array of strings exactly, and `gt(10)` matches any integer greater than 10.

Handling Overlapping Matchers

If you have multiple stubbings with overlapping matchers, Mockito will use the first stubbing it encounters. To avoid issues with overlapping matchers, you can use custom matchers or answers. For example, you might create a custom matcher that excludes certain values:

java
when(mock.method(argThat(new CustomMatcher1()))).thenReturn(result1);
when(mock.method(argThat(new CustomMatcher2()))).thenReturn(result2);

However, be cautious when using multiple custom matchers, as Mockito might throw a `NullPointerException` if the matchers are not properly defined.

Best Practices

- Use matchers for all arguments: When using any argument matchers, ensure that all arguments in the method call are specified with matchers.
- Avoid overlapping matchers: If possible, design your stubbings to avoid overlapping conditions.
- Use custom matchers: For complex conditions, consider implementing custom matchers to ensure precise control over when a stubbing is triggered.

By following these guidelines, you can effectively combine multiple argument matchers in Mockito to create robust and flexible test stubs.

Citations:
[1] https://www.digitalocean.com/community/tutorials/mockito-argument-matchers-any-eq
[2] https://www.toptal.com/java/a-guide-to-everyday-mockito
[3] https://stackoverflow.com/questions/15303177/how-to-pass-in-multiple-argumentmatchers-to-mockito
[4] https://stackoverflow.com/questions/40246106/how-does-mockito-handle-overlapping-matchers-with-multiple-arguments-in-the-then
[5] https://rieckpil.de/creating-deep-stubs-with-mockito-to-chain-method-stubbing/
[6] https://mockk.io
[7] https://www.softwaretestinghelp.com/mockito-matchers/
[8] https://semaphore.io/community/tutorials/stubbing-and-mocking-with-mockito-2-and-junit
[9] https://groups.google.com/g/mockito/c/JsMU-_cOIs8
[10] https://stackoverflow.com/questions/13846837/using-multiple-argumentmatchers-on-the-same-mock/13849384