easymock

EasyMock Verify method is used to verify that all the stubbed methods are called and there are no unexpected calls on the mocked object.

EasyMock Verify方法用于验证是否调用了所有存根方法,并且在模拟对象上没有意外调用。

EasyMock验证 (EasyMock Verify)

EasyMock verify() method has the same effect as calling verifyRecording(Object) and verifyUnexpectedCalls(Object) methods.

EasyMock verify()方法与调用verifyRecording(Object)verifyUnexpectedCalls(Object)方法具有相同的效果。

Let’s look at a simple example of EasyMock verify() method.

让我们看一下EasyMock verify()方法的一个简单示例。

package com.journaldev.easymock;import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.mock;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;import java.util.ArrayList;import org.junit.jupiter.api.Test;public class EasyMockVerifyExample {@Testpublic void test() {ArrayList<Integer> mockList = mock(ArrayList.class);expect(mockList.add(10)).andReturn(true);expect(mockList.add(20)).andReturn(true);expect(mockList.size()).andReturn(2);expect(mockList.get(0)).andReturn(10);replay(mockList);mockList.add(10);mockList.add(20);assertTrue(mockList.get(0) == 10);assertEquals(mockList.size(), 2);verify(mockList);}
}

Notice that all the stubbed methods are being called after replay() method.

请注意,所有存根方法都在replay()方法之后被调用。

What if we don’t invoke a stubbed method?

如果我们不调用存根方法怎么办?

It will throw AssertionError. For example, if I comment mockList.add(10) statement then below exception stack trace will be reported by JUnit.

它将引发AssertionError。 例如,如果我注释了mockList.add(10)语句,则下面的异常堆栈跟踪将由JUnit报告。

java.lang.AssertionError: Expectation failure on verify:ArrayList.add(10 (int)): expected: 1, actual: 0at org.easymock.internal.MocksControl.verify(MocksControl.java:284)at org.easymock.EasyMock.verify(EasyMock.java:2041)at com.journaldev.easymock.EasyMockVerifyExample.test(EasyMockVerifyExample.java:31)

EasyMock使用Nice Mock验证意外呼叫 (EasyMock verify unexpected calls with Nice Mock)

If the mocked object is a nice mock, then verify() method will not throw an error for unexpected method calls. However, it will throw an error if stubbed calls are not invoked.

如果模拟对象是一个不错的模拟对象,那么verify()方法将不会因意外的方法调用而引发错误。 但是,如果未调用存根调用,它将引发错误。

Below code snippet for test will pass with green colors because mocked object is nice mock.

下面的测试代码片段将以绿色传递,因为模拟对象是不错的模拟对象。

ArrayListmockList = mock(MockType.NICE, ArrayList.class);
replay(mockList);
mockList.add(10); //unexpected method call
verify(mockList);

EasyMock verify()复杂示例 (EasyMock verify() Complex Example)

We can move EasyMock verify() method to the @After methods to make sure all the stubbed methods were being called.

我们可以将EasyMock verify()方法移至@After方法,以确保调用了所有存根方法。

package com.journaldev.easymock;import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.mock;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import static org.junit.jupiter.api.Assertions.assertTrue;import java.util.ArrayList;import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;public class EasyMockVerifyRealExample {ArrayList<Integer> mockList;@BeforeEachpublic void setup() {mockList = mock(ArrayList.class);}@Testpublic void test() {expect(mockList.add(10)).andReturn(true);expect(mockList.get(0)).andReturn(10);replay(mockList);mockList.add(10);assertTrue(mockList.get(0) == 10);}@AfterEachpublic void teardown() {verify(mockList);}}

摘要 (Summary)

EasyMock verify() method is used to make sure that all the stubbed methods are being utilized and there are no unexpected calls. The behavior for unexpected calls changes for nice mock objects where it doesn’t throw any error. EasyMock verify() method is very similar to Mockito verify() method.

EasyMock verify()方法用于确保使用了所有已存根方法,并且没有意外调用。 意外调用的行为会更改不会引发任何错误的精美模拟对象。 EasyMock verify()方法与Mockito verify()方法非常相似。

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

翻译自: https://www.journaldev.com/22228/easymock-verify

easymock

easymock_EasyMock验证相关推荐

  1. form表单提交前进行ajax或js验证,校验不通过不提交

    在使用form表单进行提交数据前,需要进行数据的校验->表单的校验(如:两次密码输入是否相同)+后台数据的校验(如:账号是否存在),这个时候,如果哪步校验不通过,表单将停止提交,同时避免后台主键 ...

  2. SpringSecurity安全验证中文乱码问题

    使用SpringSecurity做安全验证时发现form表单中提交中文名会出现乱码问题. 原因是因为我在web.xml配置文件中将springSecurityFilterChain拦截器放在了 cha ...

  3. Ascend Pytorch算子功能验证

    Ascend Pytorch算子功能验证 编写测试用例 以add算子为例,测试脚本文件命名为:add_testcase.py.以下示例仅为一个简单的用例实现,具体算子的实现,需要根据算子定义进行完整的 ...

  4. 在OpenShift平台上验证NVIDIA DGX系统的分布式多节点自动驾驶AI训练

    在OpenShift平台上验证NVIDIA DGX系统的分布式多节点自动驾驶AI训练 自动驾驶汽车的深度神经网络(DNN)开发是一项艰巨的工作.本文验证了DGX多节点,多GPU,分布式训练在DXC机器 ...

  5. 人脸真伪验证与识别:ICCV2019论文解析

    人脸真伪验证与识别:ICCV2019论文解析 Face Forensics++: Learning to Detect Manipulated Facial Images 论文链接: http://o ...

  6. 如何写出安全的API接口(参数加密+超时处理+私钥验证+Https)

    上篇文章说到接口安全的设计思路,如果没有看到上篇博客,建议看完再来看这个. 通过园友们的讨论,以及我自己查了些资料,然后对接口安全做一个相对完善的总结,承诺给大家写个demo,今天一并放出. 对于安全 ...

  7. [JAVA EE] Thymeleaf 高级用法:模板布局,带参数的引用片段,表单验证,常用校验注解

    模板布局 公共部分通常定义为模板布局:如页眉,页脚,公共导航栏.菜单等. 模板布局定义方法 布局页中用 th:fragment 定义模板片段,其他页面用 th:insert 引用片段 例如:foote ...

  8. float js 正则 验证_使用HTML和Vuejs进行表单验证

    他们说大多数网络应用只是HTML表单.好吧,表单需要验证,谢天谢地,HTML5带有许多优秀的内置表单验证功能,可用于电子邮件,数字,最大值,分钟等.您甚至可以使用模式编写自己的验证规则.在本文中,我将 ...

  9. JQuery插件,轻量级表单模型验证(续 二)

    好不容易,有心思,那就把没做完的JQuery轻量级表单验证做完吧 之前做到了空参数验证的,现在增加带参数的验证. 附上html <form id="ValidataForm" ...

最新文章

  1. Scrum Master是什么?Scrum Master的职责是什么?和PM又有哪些区别?
  2. SharePoint 2010 技巧系列: 控制Ribbon菜单权限(SiteActions的例子)
  3. ERROR 1222 (21000): The used SELECT statements have a different number of columns :
  4. 编译包含Google Play服务App的SDK版本问题
  5. python init self_转载--------Python中:self和__init__的含义 + 为何要有self和__init__
  6. linux看门狗设备,基于Linux构建无人值守系统(看门狗)
  7. oracle 12c sql图形化,Oracle 12c PL/SQL程序设计终极指南
  8. sql语句返回近似值
  9. 阿里云 x 天合光能 | 不让一寸阳光偷偷溜走
  10. 微信小程序Axure元件库(2021最新版)
  11. xshell网站打不开
  12. 本特利探头330101-00-40-10-02-CN
  13. c语言 界面编程 毕业设计,基于c语言的毕业设计.docx
  14. ShaderForge-霓虹漩涡
  15. 图像的一些基本概念整理
  16. PMP之项目采购管理
  17. 简单OR复杂?机器学习专家为你解密企业风险量化模型
  18. 参加河北省研究生数学建模比赛有感
  19. H5首屏图片加载优化
  20. 期待第二届云计算大会

热门文章

  1. ajax内调用WCF服务
  2. (转)python3 安装 BeautifulSoup 提示 Unit tests have failed!
  3. 关于公司通过CMMI3级认证
  4. [转载] Python学习(五)Numpy通用函数汇总
  5. 异步复位同步释放机制-系统完美稳定
  6. python3+xlrd解析Excel
  7. Description Resource Path LocationType Java compiler level does not match the version of the instal
  8. 25-[jQuery]-ajax
  9. 面向对象 之重写重载
  10. 使用SQL Server Management Studio 创建数据库备份作业