Tutorial

Mockito Argument Matchers - any(), eq()

Published on August 3, 2022
Default avatar

By Pankaj

Mockito Argument Matchers - any(), eq()

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.

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.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors
Default avatar
Pankaj

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel