博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
easymock使用方法_EasyMock无效方法– ExpectLastCall()
阅读量:2546 次
发布时间:2019-05-11

本文共 1953 字,大约阅读时间需要 6 分钟。

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 {  @Test  public 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 test is executed.

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

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()

. 检出完整的项目和更多EasyMock示例。

翻译自:

easymock使用方法

转载地址:http://hxmzd.baihongyu.com/

你可能感兴趣的文章
oracle 查询表的定义语句
查看>>
Android 笔记之 Android 系统架构
查看>>
状压dp终极篇(状态转移的思想)
查看>>
AtCoder Grand Contest 031 B - Reversi
查看>>
完整成功配置wamp server小记
查看>>
build.gradle添加Oracle jdbc6 链接
查看>>
影响系统性能的20个瓶颈--转自开源中国
查看>>
根据ISBN获取豆瓣API提供的图书信息
查看>>
【转】Python中*args和**kwargs的区别
查看>>
git命令简单使用
查看>>
CODEFORCES 125E MST Company 巧用Kruskal算法
查看>>
C++标准库分析总结(三)——<迭代器设计原则>
查看>>
Ibatis 配置问题
查看>>
Notability 3.1 for Mac 中文共享版 – 好用的文本笔记工具
查看>>
HDU 2089 数位dp入门
查看>>
How do I resolve the CodeSign error: CSSMERR_TP_NOT_TRUSTED?
查看>>
linux下添加定时任务
查看>>
python的第三篇--安装Ubuntu、pycharm
查看>>
LeetCode 1092. Shortest Common Supersequence
查看>>
《区块链技术与应用》北京大学肖臻老师公开课 笔记
查看>>