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参数匹配

mockito参数匹配_Mockito参数匹配器– any(),eq()相关推荐

  1. easymock参数_EasyMock参数匹配器

    easymock参数 EasyMock argument matchers allow us to provide the flexible argument for matching when st ...

  2. spaCy V3.0 基于规则匹配(2)----高效的短语匹配器和依存句法匹配器

    1 短语匹配器(PhraseMatcher) 1.1 基本用法 对于需要匹配大型术语列表的情况,可以通过PhraseMatcher和创建Doc对象来代替词符匹配模式(token patterns),可 ...

  3. 参数匹配顺序——Python学习之参数(三)

    参数匹配顺序--Python学习之参数(三) 文章目录 参数匹配顺序--Python学习之参数(三) 函数参数匹配表 参数匹配顺序 keyword-only 参数的位置 参考资料 这篇博文是对上一篇博 ...

  4. 设计模式之----匹配器处理器模式(Matcher-Handler)的理解

    文章目录 1.前言 2.概念 3.应用场景 4.模式结构 5.案例实现 6.总结 1.前言 之前文章讲过策略模式,不懂的可以点击策略模式,今天再来说个策略模式的升级版(matcher-handler) ...

  5. Mockito匹配器优先

    这篇文章是意见. 让我们看一下Mockito中用于在Java中进行测试的verify方法. 示例: verify(myMock).someFunction(123) –期望在模拟ONCE上使用输入12 ...

  6. 过滤器匹配符包含单词_Hamcrest包含匹配器

    过滤器匹配符包含单词 与Hamcrest 1.2相比 ,针对Matchers类的Hamcrest 1.3 Javadoc文档为该类的几种方法添加了更多文档. 例如,四个重载的contains方法具有更 ...

  7. oppo人岗匹配测评_在测试中使用匹配器

    oppo人岗匹配测评 我们被迫在测试代码中写太多断言行的日子已经一去不复返了. 镇上有一个新的警长:assertThat和他的代理人:匹配者. 好吧,这不是什么新东西,但是无论如何,我想向您介绍匹配器 ...

  8. 参数匹配模型——Python学习之参数(二)

    参数匹配模型--Python学习之参数(二) 文章目录 参数匹配模型--Python学习之参数(二) 位置参数:从左至右进行匹配 关键字参数:通过参数名进行匹配 默认参数:为没有传入值的参数定义参数值 ...

  9. python参数类型_Python 参数类型和参数匹配模型

    Python 方法的参数种类有很多,而不是通常语言定义的那样, Python 方法的传参能力要比想象的强大很多.很多初学者可能对一些库中带 * 带 ** 的参数类型非常奇怪,但是其实这些语法正是保证 ...

  10. IMX6 LCD 参数匹配过程分析

    [IMX6Q]LCD参数匹配过程分析 2015-12-25 16:21 2072人阅读 评论(0) 收藏 举报 本文章已收录于: 分类: IMX6_Kernel(16) 作者同类文章X 版权声明:本文 ...

最新文章

  1. Method Swizzling的各种姿势
  2. html怎么查看cad文件,如何直接查看CAD格式的图纸
  3. 物理化学 焓变的计算和相变焓
  4. 鼠标指针放置上面,显示内容_使鼠标指针远离您键入的内容
  5. 实现一个压缩Remoting传输数据的Sink:CompressionSink (转载)
  6. spring-security过程分析
  7. BP神经网络模型与学习算法
  8. 【读书笔记】《博弈论》--翟文明
  9. 存储器——存储器容量扩充
  10. 二、Vue实例对象及其属性
  11. RocketMq中MessageQueue的分配
  12. Base64编码(java)
  13. 音频功率放大电路(使用过的语音方案电路记录)
  14. 微信视频号如何申请认证,流程是什么?
  15. 论文阅读笔记:Neural Belief Tracker: Data-Driven Dialogue State Tracking
  16. Jenkins RestAPI调用出现Error 403 No valid crumb was included in the request [亲测有用]
  17. 老树开新花:DLL劫持漏洞新玩法
  18. Java并发编程总结
  19. windows一段时间后发现C盘满了如何检查并清理
  20. 35岁的程序员该何去何从?拒绝给自己设限!!

热门文章

  1. RTI_DDS自定义插件开发 7 资源
  2. talib函数功能一览表
  3. 第一阶段:Java基础
  4. vmware workstation14永久激活密钥分享
  5. “舌尖上的安全”:基于区块链构建四位一体的食品安全社会共治体系
  6. 惠普HP LaserJet 1320n 打印机驱动
  7. Git基础:第九、十章 Git可视化工具 Git团队协作以及合并时的diff工具
  8. php tablesorter,插件 jQuery.tablesorter 中文API文档
  9. modbus 调试工具之modbus slave与modbus poll
  10. [c++] gdiplus绘制透明异型窗口