Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon What are the differences between any() and eq() in Mockito argument matchers


What are the differences between any() and eq() in Mockito argument matchers


In Mockito, `any()` and `eq()` are two different types of argument matchers used for stubbing and verifying behaviors in unit tests. Here's a detailed explanation of their differences:

any()

The `any()` matcher is used to match any argument of a specific type. It essentially means that the test will pass regardless of the value passed to the method being stubbed or verified. For example, `anyString()`, `anyInt()`, or `any(Object.class)` can be used to match any string, integer, or object, respectively. This is particularly useful when you don't care about the specific value of an argument but want to ensure that the method is called with any value of that type.

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

In this example, the `bool` method will return `true` for any string, integer, and object passed as arguments.

eq()

The `eq()` matcher is used to match a specific known value. It checks if the argument passed to the method is equal to the value specified in the `eq()` method, using the `equals()` method of the object. This is useful when you want to ensure that a method is called with a specific value.

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

Here, the `bool` method will return `false` only if the first argument is `"false"`, regardless of the values of the other arguments.

Key Differences

- Matching Criteria: `any()` matches any value of a specified type, while `eq()` matches a specific known value.
- Usage: Use `any()` when you don't care about the specific value of an argument, and use `eq()` when you need to match a specific value.
- Behavior: If one argument uses a matcher like `any()`, all arguments must use matchers. However, if you use `eq()` for one argument, you can use it alongside other matchers like `any()` for other arguments, but all arguments must still use matchers if any of them do.

Example Use Cases

When testing a method with multiple parameters, you might want to verify that it's called with a specific value for one parameter but don't care about the others. In such cases, you can use `eq()` for the specific value and `any()` for the others:

java
when(mockFoo.doSomething(eq("specificValue"), anyString(), anyInt())).thenReturn("result");

In summary, `any()` provides flexibility by allowing any value, while `eq()` ensures that a specific value is used, making them complementary tools in writing robust unit tests with Mockito.

Citations:
[1] https://www.digitalocean.com/community/tutorials/mockito-argument-matchers-any-eq
[2] https://stackoverflow.com/questions/22822512/how-do-mockito-matchers-work
[3] https://codingcraftsman.wordpress.com/2020/04/17/mockito-matchers-precedence/
[4] https://github.com/mockito/mockito/issues/193
[5] https://stackoverflow.com/questions/56559146/junit-using-eq-argument-matcher-vs-passing-string-directly/56572275
[6] https://www.javadoc.io/doc/org.mockito/mockito-core/1.10.19/org/mockito/Matchers.html
[7] https://javadoc.io/doc/org.mockito/mockito-core/4.9.0/org/mockito/ArgumentMatchers.html
[8] https://www.lambdatest.com/automation-testing-advisor/selenium/methods/org.mockito.ArgumentMatchers.anyDouble
[9] https://www.softwaretestinghelp.com/mockito-matchers/
[10] https://stackoverflow.com/questions/30890011/whats-the-difference-between-mockito-matchers-isa-any-eq-and-same