EasyMock的使用
1.Mock 方法是单元测试中常见的一种技术,它的主要作用是模拟一些在应用中不容易构造或者比较复杂的对象,从而把测试与测试边界以外的对象隔离开。同时也可以当调用别人的模块,而该模块又没有实现时(只提供接口),我们可以在独立的环境中测试自己的模块逻辑。
2.使用前的准备,下载所需的jar包:easymock-3.0.jar(或以上版本),junit-4.4.jar,cglib-nodep-2.1_3.jar
3.使用方法较简单。主要有以下步骤:
*•使用 EasyMock 生成 Mock 对象;
*•设定 Mock 对象的预期行为和输出;
*•将 Mock 对象切换到 Replay 状态;
*•调用 Mock 对象方法进行单元测试;
*•对 Mock 对象的行为进行验证。
测试实例:假如我有一个IStudent接口类和StudentApplication类,StudentApplication类中用到了IStudent中的没实现的方法,而我想测试StudentApplication,这时用EasyMock构造一个IStudent的Mock对象,并给要用到的的未实现的方法设定已知返回值。
code:

1 public interface IStudent {
2 public String doMethod1();
3 public String doMethod2();
4 public String doMethod3();
5
6 }


1 public class StudentApplication {
2 IStudent student=null;
3 public StudentApplication(){
4
5 }
6
7 public String doMethod(){
8 String str1=student.doMethod1();
9 String str2=student.doMethod2();
10 String str3=student.doMethod3();
11 return str1+str2+str3;
12 }
13
14 public IStudent getStudent() {
15 return student;
16 }
17
18 public void setStudent(IStudent student) {
19 this.student = student;
20 }
21
22
23
24 }


1 import main.IStudent;
2 import main.StudentApplication;
3
4 import org.easymock.EasyMock;
5 import org.junit.Assert;
6 import org.junit.Test;
7
8
9 public class testStudentApplication {
10 IStudent student;
11 StudentApplication application;
12 @Test
13 public void testdoMethod(){
14 //•使用 EasyMock 生成 Mock 对象;
15 student=EasyMock.createMock(IStudent.class);
16 //设定 Mock 对象的预期行为和输出
17 EasyMock.expect(student.doMethod1()).andReturn("a").times(1);
18 EasyMock.expect(student.doMethod2()).andReturn("b").times(1);
19 EasyMock.expect(student.doMethod3()).andReturn("c").times(1);
20 //将 Mock 对象切换到 Replay 状态
21 EasyMock.replay(student);
22 //调用 Mock 对象方法进行单元测试
23 application=new StudentApplication();
24 application.setStudent(student);
25 String getStr=application.doMethod();
26 //对 Mock 对象的行为进行验证
27 String cstr="abc";//正确的字符串
28 Assert.assertEquals(getStr, cstr);
29 EasyMock.verify(student);
30
31 }
32
33 }

EasyMock的使用的更多相关文章
- 测试--easymock的使用
使用场景:对于调用其它类中的方法,但是还没有编写完,使用easymock进行单元测试,它提供这些没有编写完的代码期待的默认值. 使用步骤: step1: pom引入: <dependency&g ...
- Mock之easymock, powermock, and mockito
easymock, powermock, and mockito Easymock Class Mocking Limitations To be coherent with interface mo ...
- easymock所测试的方法内部新NEW对象的处理
问题:当记录的方法的参数是方法所在类内部新NEW的对象时,静态的记录方法交互就会失效,例如 调用的方法: public calss A{ public void method(User u){ u.s ...
- easymock+junit+spring学习·
Easymock学习 Author:luojie 1. Easymock简介 EasyMock 是一套通过简单的方法对于指定的 ...
- PowerMock与EasyMock的应用(转)
Leader请求在做Junit测试的时辰,Mock掉各个办法之间的依附.这两天进修了下PowerMock的应用. PowerMock是EasyMock的一个扩大,参加了static,final,pri ...
- 窥探EasyMock(1)基础使用篇
EasyMock的应用分为5步: 1. 使用 EasyMock 生成 Mock 对象: SomeInterface mockObj = createMock(SomeInterface.class); ...
- 窥探EasyMock(2)进阶使用篇
from:http://www.iteye.com/topic/310313 1. 生成 Mock 对象 如何创建一个需要严格遵守调用顺序的mock对象? SomeInterface mockObj ...
- easymock入门贴
from:http://macrochen.iteye.com/blog/298032 关于EasyMock常见的几个问题, 这里(http://ozgwei.blogspot.com/2007/06 ...
- 使用 EasyMock 更轻松地进行测试
from:http://www.ibm.com/developerworks/cn/java/j-easymock.html 测试驱动开发是软件开发的重要部分.如果代码不进行测试,就是不可靠的.所有代 ...
- EasyMock 使用方法与原理剖析
from:http://www.ibm.com/developerworks/cn/opensource/os-cn-easymock/ Mock 方法是单元测试中常见的一种技术,它的主要作用是模拟一 ...
随机推荐
- cocos2dx 3.0 中文 iconv 转换函数
//#include <string> #pragma once #include "cocos2d.h"; #include "iconv\include\ ...
- spring---aop(10)---Spring AOP中AspectJ
写在前面 在之前的文章中有写到,Spring在配置中,会存在大量的切面配置.然而在很多情况下,SpringAOP 所提供的切面类真的不是很够用,比如想拦截制定的注解方法,我们就必须扩展DefalutP ...
- PCI DSS合规建设ASV扫描介绍
最近查一些Nessus.Nexpose漏洞扫描工具相关资料,工具介绍都会提到一些审计功能,其中最常见的就是PCI DSS合规性审计.从网上找到一篇介绍较详尽的文章,与大家分享. 原文摘自:http:/ ...
- c++ 常见网络协议头
//NTP协议 typedef struct _NTP_HEADER { uint8_t _flags;//Flags 0xdb uint8_t _pcs;//Peer Clock Stratum u ...
- Spring Boot项目的Logback配置文件使用yaml格式
1.普通的Spring项目使用logback默认用properties文件做为配置变量. 2.如果非要用yaml文件,那么可以转成Spring Boot项目,天生无缝结合 3.没办法,如果项目配置文件 ...
- Maven的POM文件parent节点不可以使用properties里面的变量
Maven的POM文件parent节点不可以使用properties里面的变量 但是如果在子项目上的parent节点是可以使用父项目里定义的properties变量 如果一开始为单项目,或者最顶层项目 ...
- MySQL数据库事务各隔离级别加锁情况--read committed && MVCC(转)
本文转自https://m.imooc.com/article/details?article_id=17290 感谢作者 上篇记录了我对MySQL 事务 隔离级别read uncommitted的理 ...
- 安全:Web 安全学习笔记
背景 说来惭愧,6 年的 web 编程生涯,一直没有真正系统的学习 web 安全知识(认证和授权除外),这个月看了一本<Web 安全设计之道>,书中的内容多是从微软官方文档翻译而来,这本书 ...
- Selenium2+python自动化35-获取元素属性
前言 通常在做断言之前,都要先获取界面上元素的属性,然后与期望结果对比.本篇介绍几种常见的获取元素属性方法. 一.获取页面title 1.有很多小伙伴都不知道title长在哪里,看下图左上角. 2.获 ...
- 解决kylin sync table报错:MetaException(message:java.lang.ClassNotFoundException Class org.apache.hive.hcatalog.data.JsonSerDe not found
在kylin-gui中sync表default.customer_visit时报错: -- ::, ERROR [http-bio--exec-] controller.BasicController ...