easymock使用方法

Sometimes we want to mock void methods. EasyMock expect() method can’t be used to mock void methods. However, we can use expectLastCall() along with andAnswer() to mock void methods.

有时我们想模拟void方法。 EasyMock Expect expect()方法不能用于模拟void方法。 但是,我们可以使用expectLastCall()连同andAnswer()嘲笑无效的方法。

EasyMock无效方法 (EasyMock void method)

When we use expectLastCall() and andAnswer() to mock void methods, we can use getCurrentArguments() to get the arguments passed to the method and perform some action on it. Finally, we have to return null since we are mocking a void method.

当我们使用andAnswer() expectLastCall()andAnswer()来模拟void方法时,可以使用getCurrentArguments()来获取传递给该方法的参数并对其执行一些操作。 最后,由于要模拟void方法,因此必须返回null

Let’s say we have a utility class as:

假设我们有一个实用程序类:

package com.journaldev.utils;public class StringUtils {public void print(String s) {System.out.println(s);}
}

Here is the code to mock void method print() using EasyMock.

这是使用EasyMock模拟void方法print()的代码。

package com.journaldev.easymock;import static org.easymock.EasyMock.*;import org.junit.jupiter.api.Test;import com.journaldev.utils.StringUtils;public class EasyMockVoidMethodExample {@Testpublic void test() {StringUtils mock = mock(StringUtils.class);mock.print(anyString());expectLastCall().andAnswer(() -> {System.out.println("Mock Argument = "+getCurrentArguments()[0]);return null;}).times(2);replay(mock);mock.print("Java");mock.print("Python");verify(mock);}
}

Below image shows the console output when the above JUnit test is executed.

下图显示了执行上述JUnit测试时的控制台输出。

ExpectLastCall()。Void() (expectLastCall().andVoid())

If we just want to mock void method and don’t want to perform any logic, we can simply use expectLastCall().andVoid() right after calling void method on mocked object.

如果我们只想模拟void方法并且不想执行任何逻辑,则可以在对模拟对象调用void方法之后expectLastCall().andVoid()使用expectLastCall().andVoid()

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

翻译自: https://www.journaldev.com/22287/easymock-void-method-expectlastcall

easymock使用方法

easymock使用方法_EasyMock无效方法– ExpectLastCall()相关推荐

  1. easymock使用方法_EasyMock最终方法– PowerMock,JUnit 4,TestNG

    easymock使用方法 One of the limitations of EasyMock is that it can't mock final methods and final classe ...

  2. mockito无效_Mockito模拟无效方法

    mockito无效 Most of the times Mockito when() method is good enough to mock an object's behavior. But w ...

  3. Windows Server 2003 SP2无法安装解决方法(密钥无效)

    原文地址:Windows Server 2003 SP2无法安装解决方法(密钥无效)作者:逆水行舟 Windows Server 2003 Service Pack 2 无法安装.用来安装 Micro ...

  4. php框架m方法详细,Thinkphp框架中D方法与M方法的区别是什么

    Thinkphp框架中D方法与M方法的区别是什么 发布时间:2021-02-06 23:09:38 来源:亿速云 阅读:69 作者:小新 小编给大家分享一下Thinkphp框架中D方法与M方法的区别是 ...

  5. php大m大d,ThinkPHP中M方法与D方法区别

    [前言] ? ? ? 本文总结下ThinkPHP中M方法与D方法区别,以下均为个人见解.如有误解请指正 ? [主体] (一)个人见解: ? ? ? ?M()方法参数可选,有参数时表示实例化父类模型的同 ...

  6. Spirng的事务 方法A调用方法B,事务是否失效

    总结: 方法A调用方法B: 如果A和B方法在同一个类中: 如果A加@Transactional注解,B加不加@Transactional注解,事务是有效的,则AB在同一事务中. 如果A不加@Trans ...

  7. AUV光源优化方法+水下图像增强方法

    <Underwater image enhancement framework and its application on an autonomous underwater vehicle p ...

  8. python contains魔法方法_python魔法方法

    Python 魔术方法指南 入门 构造和初始化 构造定制类用于比较的魔术方法 用于数值处理的魔术方法 表现你的类 控制属性访问 创建定制序列 反射 可以调用的对象 会话管理器 创建描述器对象 持久化对 ...

  9. php json.parse,JSON.parse() 方法与$.parseJSON()方法

    JSON.parse() 方法与$.parseJSON()方法 1. JSON.parse() JSON.parse() 方法解析一个JSON字符串 参数 text 要被解析成JavaSctipt值的 ...

最新文章

  1. 惊艳了!升级版的 APDrawing,秒让人脸照变线条肖像画
  2. 程序员的量化交易之路(1)----规划开篇
  3. zigbee 串口不稳定_Zigbee
  4. net Core做一个webApi的简单实例
  5. hdu-5493 Queue(二分+树状数组)
  6. 库克微微一笑:苹果市值再次突破万亿美元大关
  7. php 启动管理工具下载,PHP管理工具compser windows下安装
  8. python开发工具和框架安装器_Python基础框架和工具
  9. java 数组或者链表输出
  10. 如何切图PS切图&网页切图
  11. Ubuntu 部署Django, Wagtail,Postgres, Nginx, Gunicorn,Supervisor
  12. 路由器无线中继WDS\WISP\CLINENT+AP优劣
  13. python矩阵教程_Python Numpy Tutorial / Python Numpy 教程 (矩阵和图像操作)
  14. 总结知识,提高认知--牛腩总结
  15. JAVA虚拟机栈的主要特点
  16. Latex表格线宽修改方法以及内容左对齐。
  17. 校招潜规则,泄密太多我担心大厂HR封杀我
  18. LSA(Latent semantic analysis)
  19. 谷粒商城P44的Vue模板
  20. 软件环境、硬件环境、开发工具

热门文章

  1. ajax图片上传,基于firefox
  2. [原]LVM管理与虚拟机管理
  3. 四川专利代理机构列表
  4. [转载] python中append函数的用法
  5. 【图像处理】openCV库教程
  6. linux修改主机名(不重启)
  7. python---之super()继承,解决钻石继承难题
  8. OpenIN2 Linux 编译OpenCV 报错undefined reference to `xxx@xxx‘
  9. 在CMakeLists.txt文件中包含Eigen
  10. html div右下浮动,div 初始化及左右浮动