Mockito Argument Matchers – any(), eq()

Filed Under: Mockito

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.

Mockito Argument Matchers – any()

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()));

Mockito Argument Matcher – eq()

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 AdditionalMatchers

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 Verify Argument Matchers

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));

Summary

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages