Mockito allows us to create mock objects and stub the behavior for our test cases. We usually mock the behavior using when()
and thenReturn()
on the mock object.
Sometimes we want to mock the behavior for any argument of the given type, in that case, we can use Mockito argument matchers. Mockito argument methods are defined in org.mockito.ArgumentMatchers
class as static methods. Let’s say we have a class defined as:
class Foo {
boolean bool(String str, int i, Object obj) {
return false;
}
int in(boolean b, List<String> strs) {
return 0;
}
int bar(byte[] bytes, String[] s, int i) {
return 0;
}
}
Let’s see some examples of using mockito argument matchers to stub generic behaviors.
Foo mockFoo = mock(Foo.class);
when(mockFoo.bool(anyString(), anyInt(), any(Object.class))).thenReturn(true);
We are stubbing bool() method to return “true” for any string, integer and object arguments. All the below assertions will pass in this case:
assertTrue(mockFoo.bool("A", 1, "A"));
assertTrue(mockFoo.bool("B", 10, new Object()));
When we use argument matchers, then all the arguments should use matchers. If we want to use a specific value for an argument, then we can use eq()
method.
when(mockFoo.bool(eq("false"), anyInt(), any(Object.class))).thenReturn(false);
assertFalse(mockFoo.bool("false", 10, new Object()));
There are argument matchers for the list, set, and map too.
when(mockFoo.in(anyBoolean(), anyList())).thenReturn(10);
If you want to match with arrays, then use any() method like this:
any(byte[].class)
any(Object[].class)
Mockito org.mockito.AdditionalMatchers
class provides some rarely used matchers. We can specify arguments to be greater than, less than, perform OR, AND, NOT operations. We can also check for equality of arrays.
when(mockFoo.bar(any(byte[].class), aryEq(new String[] { "A", "B" }), gt(10))).thenReturn(11);
So if we call bar() method with any byte array as argument, second argument as { “A”, “B” } and third argument greater than 10, then the stubbed method will return 11. Below assertions will pass for our stubbed method.
assertEquals(11, mockFoo.bar("abc".getBytes(), new String[] { "A", "B" }, 20));
assertEquals(11, mockFoo.bar("xyz".getBytes(), new String[] { "A", "B" }, 99));
Mockito argument matchers can be used only with when() and verify() methods. Let’s look at a few examples of using argument matchers in Mockito verify method.
verify(mockFoo, atLeast(0)).bool(anyString(), anyInt(), any(Object.class));
verify(mockFoo, atLeast(0)).bool(eq("false"), anyInt(), any(Object.class));
Mockito argument matcher methods are very useful in stubbing behaviors in a generic way. There are many methods to cover almost all the requirements.
You can look at more Mockito examples from our GitHub Repository.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.