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 方法是单元测试中常见的一种技术,它的主要作用是模拟一 ...
随机推荐
- mysql关联查询和联合查询
一.内联方式 1.传统关联查询 "select * from students,transcript where students.sid=transcript.sid and transc ...
- MYSQL-5.5.37-win32.msi 这个版本得程序包谁有吗 可以给我一下吗?
之前下载了这个版本得mysql 但是跟服务器链接不上 后来我就卸载了 但由于卸载不干净 现在又删了注册表 好像把这个程序包得什么文件删除了 现在提示配置文件错误 所以有这个程序包得 ...
- 3D单机游戏《天鹰教》源码发布(二)
3D单机游戏<天鹰教>源码发布 作者 作者: 游蓝海 博客: http://blog.csdn.net/you_lan_hai mail: you_lan_hai@foxmail. ...
- Android实现两次按下返回键退出
@Override public boolean onKeyDown(int keyCode, KeyEvent event) { if(keyCode == KeyEvent.KEYCODE_BAC ...
- highcharts 画图选项配置(待完善------)
在使用highcharts画图过程中,经常查阅图表选项设置,现将画图过程中设置过的选项收集记录如下留待以后参考: 折线图 <!DOCTYPE html> <html lang=&qu ...
- Android系列教程(十六) 在电脑上装Android
[软件准备] 1.LiveAndroid v0.3 liveCD [点击下载] 2.VirtualBox 3.0.4 [点击下载] [图片安装流程] 主要安装思路为:通过vir ...
- .NET:CLR via C# Assembly Loading
基础知识 Internally, the CLR attempts to load this assembly by using the System.Reflection.Assembly clas ...
- 【BZOJ】【4066】简单题(强制在线)
KD-Tree KD-Tree的进阶姿势戳这里 http://zyfzyf.is-programmer.com/posts/92431.html 为啥有种线段树&平衡树的即视感……(树形结构的 ...
- spark调优经验(待续)
spark调优是须要依据业务须要调整的,并非说某个设置是一成不变的,就比方机器学习一样,是在不断的调试中找出当前业务下更优的调优配置.以下零碎的总结了一些我的调优笔记. spark 存储的时候存在严重 ...
- Redhat Linux FTP配置
文件传输协议(FTP:FileTransfer Protocol)使得主机间可以共享文件. FTP 使用 TCP 生成一个虚拟连接用于控制信息,然后再生成一个单独的 TCP 连接用于数据传输.控制连接 ...