Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon What are some advanced use cases for Mockito argument matchers


What are some advanced use cases for Mockito argument matchers


Mockito argument matchers are powerful tools used in unit testing to provide flexibility when stubbing or verifying method calls. They allow you to define how arguments should be matched during these operations, enabling more generic and robust tests. Here are some advanced use cases for Mockito argument matchers:

1. Using `any()` and Specific Values with `eq()`**

When you want to match some arguments generically while specifying others precisely, you can combine `any()` with `eq()`. For example, if you have a method `bool(String str, int i, Object obj)`, you can stub it to return `true` for any string, a specific integer, and any object:

java
when(mockFoo.bool(anyString(), eq(10), any(Object.class))).thenReturn(true);

This setup will return `true` for any string, when the integer is `10`, and for any object passed as the third argument.

2. Custom Argument Matching with `argThat()`**

If the built-in matchers are insufficient, you can create custom matchers using `argThat()`. This method allows you to pass an `ArgumentMatcher` that defines your custom logic for matching arguments. For instance, you might want to match a string that starts with a certain prefix:

java
when(mockFoo.bool(argThat(new ArgumentMatcher() {
    @Override
    public boolean matches(String argument) {
        return argument.startsWith("prefix");
    }
}), anyInt(), any(Object.class))).thenReturn(true);

3. Matching Arrays with `aryEq()`**

When dealing with array arguments, you can use `aryEq()` to match arrays exactly. This is particularly useful when you need to verify that a method was called with a specific array:

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

In this example, the method `bar()` will return `11` if called with any byte array, the string array `["A", "B"]`, and an integer greater than `10`.

4. Using `longThat()`, `floatThat()`, and `doubleThat()` for Custom Primitive Matching**

These methods allow you to define custom matchers for primitive types like `long`, `float`, and `double`. You can use them to match numbers based on specific conditions, such as being greater than a certain value or matching a pattern:

java
when(mockFoo.method(longThat(new ArgumentMatcher() {
    @Override
    public boolean matches(Long argument) {
        return argument > 100;
    }
}))).thenReturn(true);

5. Verification Matchers**

Mockito argument matchers are not limited to stubbing; they can also be used for verification. You can use them with `verify()` to check if a method was called with specific arguments. For example:

java
verify(mockFoo, atLeast(1)).bool(anyString(), eq(10), any(Object.class));

This verifies that the `bool()` method was called at least once with any string, the integer `10`, and any object.

6. Handling Null and Non-Null Arguments**

You can use `isNull()` and `isNotNull()` to match null or non-null arguments, respectively. This is useful when you need to handle methods that might be called with null values:

java
when(mockFoo.method(isNull())).thenReturn(true);

7. Regular Expression Matching with `matches()`**

The `matches()` method allows you to match strings against regular expressions. This can be useful for verifying that a method was called with a string that matches a certain pattern:

java
when(mockFoo.method(matches("^[a-zA-Z]+$"))).thenReturn(true);

In this example, the method will return `true` if called with a string consisting entirely of letters.

These advanced use cases demonstrate how Mockito argument matchers can enhance the flexibility and robustness of your unit tests by allowing you to define complex matching rules for method arguments.

Citations:
[1] https://www.digitalocean.com/community/tutorials/mockito-argument-matchers-any-eq
[2] https://www.softwaretestinghelp.com/mockito-matchers/
[3] https://mockk.io
[4] https://www.lambdatest.com/automation-testing-advisor/selenium/methods/org.mockito.ArgumentMatchers.longThat
[5] https://stackoverflow.com/questions/22822512/how-do-mockito-matchers-work
[6] https://www.lambdatest.com/automation-testing-advisor/selenium/methods/org.mockito.ArgumentMatchers.matches
[7] https://www.tpointtech.com/mockito-argument-matchers
[8] https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/ArgumentMatchers.html