easymock参数

EasyMock argument matchers allow us to provide the flexible argument for matching when stubbing the methods. You will find a lot of any*() methods in EasyMock that can be used with expect() to provide arguments for a method call.

EasyMock参数匹配器使我们可以在对方法进行存根时提供灵活的参数以进行匹配。 您将在EasyMock中找到许多any*()方法,这些方法可以与expect()一起使用,以提供方法调用的参数。

EasyMock参数匹配器示例 (EasyMock Argument Matchers Example)

Let’s look at a simple example of mocking ArrayList and stubbing its behaviors for any argument of a specific type.

让我们看一个模拟ArrayList并将其行为存入特定类型的任何参数的简单示例。

package com.journaldev.easymock;import static org.easymock.EasyMock.*;
import static org.junit.jupiter.api.Assertions.*;import java.util.ArrayList;import org.junit.jupiter.api.Test;public class EasyMockAgrumentMatcherExample {@Testpublic void test() {ArrayList<Object> mockList = mock(ArrayList.class);expect(mockList.add(anyInt())).andReturn(true);expect(mockList.add(anyString())).andReturn(false);replay(mockList);assertTrue(mockList.add(10));assertFalse(mockList.add("Hi"));verify(mockList);}
}

Notice the use of anyInt() and anyString() for specifying the argument as any int or string object.

注意,使用anyInt()anyString()将参数指定为任何int或字符串对象。

Apart from matching any argument, there are some other argument matchers too. Let’s look at some of these argument matchers with example code.

除了匹配任何参数之外,还有其他一些参数匹配器。 让我们来看一些带有示例代码的参数匹配器。

平等论者匹配器 (Equality Argument Matcher)

We can use same() argument matcher method for stubbing behavior with same object. If you want to perform equality check, use eq() method.

我们可以使用same()参数匹配器方法对同一个对象的行为进行存根。 如果要执行相等性检查,请使用eq()方法。

package com.journaldev.easymock;import static org.easymock.EasyMock.*;
import static org.junit.jupiter.api.Assertions.*;import java.util.ArrayList;import org.junit.jupiter.api.Test;public class EasyMockAgrumentMatcherEqualityExample {@Testpublic void test() {Utils mock = mock(Utils.class);Object obj = new Object();expect(mock.convert(same(obj))).andReturn("True");expect(mock.convert(eq("ABC"))).andReturn("Awesome");expect(mock.convert(anyObject())).andReturn("False");replay(mock);assertEquals("True", mock.convert(obj));assertEquals("Awesome", mock.convert("ABC"));assertEquals("False", mock.convert(new Object()));}
}class Utils {public String convert(Object obj) {return obj.toString();}
}

比较参数匹配器 (Comparison Argument Matcher)

We can use lt(), gt(), leq() and geq() methods to compare arguments and return specific output. Note that these methods are overloaded to use with primitive data types as well as objects. If you are using them with any Object, then they should be Comparable.

我们可以使用lt()gt()leq()geq()方法来比较参数并返回特定的输出。 请注意,这些方法被重载以与原始数据类型以及对象一起使用。 如果将它们与任何对象一起使用,则它们应该是Comparable

package com.journaldev.easymock;import static org.easymock.EasyMock.*;
import static org.junit.jupiter.api.Assertions.*;import java.util.ArrayList;
import java.util.List;import org.junit.jupiter.api.Test;public class EasyMockAgrumentMatcherComparisonExample {@Testpublic void test() {Listmock = mock(ArrayList.class);expect(mock.add(lt(10))).andReturn(true);expect(mock.add(geq(10))).andReturn(false);replay(mock);assertTrue(mock.add(5));assertFalse(mock.add(100));}
}

条件参数匹配器 (Conditional Argument Matchers)

We can use and(), or() and not() functions with argument matchers to write more complex behaviors.

我们可以将and()or()not()函数与参数匹配器一起使用,以编写更复杂的行为。

Note that when we are using argument matchers to stub behaviors, every argument has to be a matcher otherwise, we will get an error message. So if we have to specify not(100), then write it as not(eq(100)).

请注意,当我们使用参数匹配器对行为进行存根时,每个参数都必须是匹配器,否则,我们将收到一条错误消息。 因此,如果我们必须指定not(100) ,则将其写为not(eq(100))

Here is a simple example of using conditional argument matchers.

这是使用条件参数匹配器的简单示例。

package com.journaldev.easymock;import static org.easymock.EasyMock.*;
import static org.junit.jupiter.api.Assertions.*;import java.util.ArrayList;
import java.util.List;import org.junit.jupiter.api.Test;public class EasyMockAgrumentMatcherConditionalExample {@Testpublic void test() {List<Integer> mock = mock(ArrayList.class);//return true if number is between 0 to 10expect(mock.add(and(gt(0), lt(10)))).andReturn(true);//return true if number is 33 or 77expect(mock.add(or(eq(33), eq(77)))).andReturn(true);//return true if number is not 99expect(mock.add(not(lt(100)))).andReturn(false);        replay(mock);assertTrue(mock.add(5));assertTrue(mock.add(33));assertFalse(mock.add(102));}
}

空参数匹配器 (Null Argument Matchers)

We can use isNull() and notNull() to match null and not-null arguments. However, isNull() comes handy when we are using other argument matchers and can’t use null directly.

我们可以使用isNull()notNull()来匹配null和notNull()参数。 但是,当我们使用其他参数匹配器时, isNull()会派上用场,并且不能直接使用null。

List<Object> mock = mock(ArrayList.class);expect(mock.add(isNull())).andReturn(true);
expect(mock.add(notNull())).andReturn(false);
replay(mock);assertTrue(mock.add(null));
assertFalse(mock.add("Hi"));

字符串参数匹配器 (String Argument Matchers)

There are few utility methods especially for matching String arguments. These are:

实用程序方法很少,特别是用于匹配String参数的方法。 这些是:

  • startsWith(String)startsWith(String)
  • endsWith(String)EndsWith(String)
  • contains(String)contains(String)
  • matches(Regex): Expects argument string to match the regex.matchs(Regex):期望参数字符串与正则表达式匹配。
  • find(Regex): Expects one of the substring to match the regex.find(Regex):期望子字符串之一与正则表达式匹配。

Here is a simple example showing usage of string argument matchers.

这是一个简单的示例,显示了字符串参数匹配器的用法。

List<String> mock = mock(ArrayList.class);expect(mock.add(startsWith("Java"))).andReturn(true);
expect(mock.add(endsWith("Dev"))).andReturn(true);
expect(mock.add(contains("Pyt"))).andReturn(true);
expect(mock.add(matches("^[abc]d."))).andReturn(true);
expect(mock.add(find("[9]{3}"))).andReturn(true);replay(mock);//startsWith Java
assertTrue(mock.add("Java World"));
//endsWith Dev
assertTrue(mock.add("JournalDev"));
//contains Pyt
assertTrue(mock.add("Python"));
//matches ads
assertTrue(mock.add("ads"));
// 999 is one of substring
assertTrue(mock.add("ABC999DDD")); verify(mock);

自定义参数匹配器 (Custom Argument Matcher)

Although EasyMock argument matchers support is extensive, if you want to implement a custom argument matcher then you can implement IArgumentMatcher interface and use it through EasyMock.reportMatcher(IArgumentMatcher).

尽管EasyMock参数匹配器支持广泛,但如果要实现自定义参数匹配器,则可以实现IArgumentMatcher接口,并通过EasyMock.reportMatcher(IArgumentMatcher)使用它。

GitHub Repository.GitHub存储库中检出完整的项目和更多EasyMock示例。

翻译自: https://www.journaldev.com/22292/easymock-argument-matchers

easymock参数

easymock参数_EasyMock参数匹配器相关推荐

  1. easymock参数_EasyMock捕获参数

    easymock参数 Sometimes we want to stub behaviors for any input arguments, so we use argument matchers. ...

  2. Jest 测试框架 expect 和 匹配器 matcher 的设计原理解析

    副标题:SAP Spartacus SSR 优化的单元测试分析之二 - 调用参数检测 源代码: it(`should pass parameters to the original engine in ...

  3. Mockito匹配器优先

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

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

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

  5. 3D人脸查看器和匹配器

    目录 背景 技术 Mesh 文件 人脸图像 面部拟合 相机,灯光,动作 用户界面 代码高亮 演示 注意 更新 4.0版 Face Matcher的代码高亮 创建FaceServiceClient对象 ...

  6. 【图像配准】多图配准/不同特征提取算法/匹配器比较测试

    前言 本文首先完成之前专栏前置博文未完成的多图配准拼接任务,其次对不同特征提取器/匹配器效率进行进一步实验探究. 各类算法原理简述 看到有博文[1]指出,在速度方面SIFT<SURF<BR ...

  7. Jest测试框架入门之匹配器与测试异步代码

    一.匹配器 1.对于一般的数字与字符串类型使用 toBe test('adds 1 + 2 to equal 3', () => {expect(1 + 2).toBe(3); });test( ...

  8. Jest 常用匹配器

    Jest 常用匹配器 toBe toBe 使用 Object.is 判断是否严格相等.我理解为精准匹配 test ('测试10与10相匹配',()=>{// toBe 匹配引用地址相同expec ...

  9. AntPathMatcher路径匹配器

    有志者,事竟成 文章持续更新,可以关注[小奇JAVA面试]第一时间阅读,回复[资料]获取福利,回复[项目]获取项目源码,回复[简历模板]获取简历模板,回复[学习路线图]获取学习路线图. 文章目录 一. ...

最新文章

  1. Zend Framework Mail通过网易免费邮箱发送邮件
  2. 怎么快速插入 100 条数据,用时最短
  3. ViewState与Session 的重要区别
  4. 同是产品经理,为什么几年后会差距这么大?
  5. MySQL主从复制性能优化
  6. nginx 源码学习笔记(十)——基本容器——ngx_hash
  7. css检测,CSS检测工具 CSS Lint简介
  8. linux 用户权限 数字,几个linux命令之用户权限相关命令
  9. c语言根号11取值两位小数,用C语言将一个数开根号后再取倒数的方法
  10. java删除表格_Java 删除Word表格/表格内容
  11. 任玉刚【Android开发艺术探索】读后笔记二
  12. 电商格局谋定重整-万祥军:李玉庭对话中国经济和信息化
  13. QQ坦白说闹得我差点分手,破解揪元凶证清白
  14. 树莓派安装HackRF、LimeSDR、GNU Radio、Gqrx
  15. Ajax读取本地json文件
  16. 尚学堂视频笔记六:多线程
  17. 电话程控交换机安装注意
  18. Zircon - Fuchsia 内核分析 - 启动(平台初始化)
  19. c#简单几步实现圆角按钮
  20. naticat连接mysql报错_「2509」Navicat连接mysql报错2509 - seo实验室

热门文章

  1. jdk配置环境变量的方法
  2. 锋利的jQuery-3--用js给多选的checkbox或者select赋值
  3. [转载] Python: struct 模块之字节对齐问题
  4. 如何保障MySQL主从复制关系的稳定性?关键词(新特性、crash-safe)
  5. 图片的色彩空间转换、简单色彩跟踪与通道分离、合并(三)
  6. python基本操作(四)
  7. 学Python Web开发框架到什么程度可以找到开发的工作?
  8. 注释驱动的 Spring cache 缓存介绍
  9. hdu 4421(枚举+2-sat)
  10. [文件、数据库、XML]window phone 利用StreamWriter写入文件问题