mockito参数匹配_Mockito参数匹配器– any(),eq()

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允许我们创建模拟对象并为测试用例添加行为。 我们通常在模拟对象上使用when()thenReturn()来模拟行为。

Mockito参数匹配器– any() (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.

有时我们想模拟给定类型的任何参数的行为,在这种情况下,我们可以使用Mockito参数匹配器。 Mockito参数方法在org.mockito.ArgumentMatchers类中定义为静态方法。

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:

我们正在对bool()方法进行存根处理以为任何字符串,整数和对象参数返回“ true”。 在这种情况下,以下所有断言都将通过:

assertTrue(mockFoo.bool("A", 1, "A"));
assertTrue(mockFoo.bool("B", 10, new Object()));

Mockito参数匹配器– eq() (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.

当我们使用参数匹配器时,所有参数都应使用匹配器。 如果要为参数使用特定值,则可以使用eq()方法。

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()方法,如下所示:

any(byte[].class)
any(Object[].class)

Mockito附加匹配项 (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.

Mockito org.mockito.AdditionalMatchers类提供了一些很少使用的匹配器。 我们可以指定参数大于,小于执行OR,AND,NOT运算。 我们还可以检查数组是否相等。

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.

因此,如果我们使用任何字节数组作为参数调用bar()方法,将第二个参数称为{“ A”,“ B”},并且第三个参数大于10,则存根方法将返回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验证参数匹配器 (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.

Mockito参数匹配器只能与when()和verify()方法一起使用。 让我们看一些在Mockito verify方法中使用参数匹配器的示例

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.

Mockito参数匹配器方法在以常规方式对行为进行存根时非常有用。 有很多方法可以满足几乎所有要求。

GitHub Repository. GitHub存储库中查看更多Mockito示例。

翻译自: https://www.journaldev.com/21876/mockito-argument-matchers-any-eq

mockito参数匹配

  • 10
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值