Mockito Argument Matchers – any(), eq()

参考文章:https://www.journaldev.com/21876/mockito-argument-matchers-any-eq
https://blog.csdn.net/cunchi4221/article/details/107475413

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值