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的使用的更多相关文章

  1. 测试--easymock的使用

    使用场景:对于调用其它类中的方法,但是还没有编写完,使用easymock进行单元测试,它提供这些没有编写完的代码期待的默认值. 使用步骤: step1: pom引入: <dependency&g ...

  2. Mock之easymock, powermock, and mockito

    easymock, powermock, and mockito Easymock Class Mocking Limitations To be coherent with interface mo ...

  3. easymock所测试的方法内部新NEW对象的处理

    问题:当记录的方法的参数是方法所在类内部新NEW的对象时,静态的记录方法交互就会失效,例如 调用的方法: public calss A{ public void method(User u){ u.s ...

  4. easymock+junit+spring学习·

    Easymock学习                                Author:luojie 1.       Easymock简介 EasyMock 是一套通过简单的方法对于指定的 ...

  5. PowerMock与EasyMock的应用(转)

    Leader请求在做Junit测试的时辰,Mock掉各个办法之间的依附.这两天进修了下PowerMock的应用. PowerMock是EasyMock的一个扩大,参加了static,final,pri ...

  6. 窥探EasyMock(1)基础使用篇

    EasyMock的应用分为5步: 1. 使用 EasyMock 生成 Mock 对象: SomeInterface mockObj = createMock(SomeInterface.class); ...

  7. 窥探EasyMock(2)进阶使用篇

    from:http://www.iteye.com/topic/310313 1. 生成 Mock 对象 如何创建一个需要严格遵守调用顺序的mock对象? SomeInterface mockObj  ...

  8. easymock入门贴

    from:http://macrochen.iteye.com/blog/298032 关于EasyMock常见的几个问题, 这里(http://ozgwei.blogspot.com/2007/06 ...

  9. 使用 EasyMock 更轻松地进行测试

    from:http://www.ibm.com/developerworks/cn/java/j-easymock.html 测试驱动开发是软件开发的重要部分.如果代码不进行测试,就是不可靠的.所有代 ...

  10. EasyMock 使用方法与原理剖析

    from:http://www.ibm.com/developerworks/cn/opensource/os-cn-easymock/ Mock 方法是单元测试中常见的一种技术,它的主要作用是模拟一 ...

随机推荐

  1. mysql关联查询和联合查询

    一.内联方式 1.传统关联查询 "select * from students,transcript where students.sid=transcript.sid and transc ...

  2. 多线程_java多线程环境下栈信息分析思路

    导读:Java多线程开发给程序带来好处的同时,由于多线程程序导致的问题也越来越多,而且对问题的查找和分析解决对于菜鸟程序原来是是件头疼的事.下面我就项目中使用多线程开发程序过程中遇到的问题做详细的分析 ...

  3. CentOS 6.9配置EPEL源

    简介: EPEL是一个由特别兴趣小组创建.维护并管理的,针对 红帽企业版 Linux(RHEL)及其衍生发行版(比如 CentOS.Scientific Linux.Oracle Enterprise ...

  4. hadoop学习;Streaming,aggregate;combiner

    hadoop streaming同意我们使用不论什么可运行脚本来处理按行组织的数据流,数据取自UNIX的标准输入STDIN,并输出到STDOUT 我们能够用 linux命令管道查看文本有多少行,cat ...

  5. Quartz实现动态定时任务

    一. 说明 由于最近工作要实现定时任务的执行,而且要求定时周期是不固定的,所以就用到了quartz来实现这个功能: spring3.1以下的版本必须使用quartz1.x系列,3.1以上的版本才支持q ...

  6. MVC扩展Filter,通过继承HandleErrorAttribute,使用log4net或ELMAH组件记录服务端500错误、HttpException、Ajax异常等

    □ 接口 public interface IExceptionFilter{    void OnException(ExceptionContext filterContext);} Except ...

  7. IPHONE IOS6 模拟器没有HOME按键解决方法

    替代home键的快捷键是 Cmd-Shift-H: 双击HOME键就是 Cmd-Shift-H 按两次: 参考:http://www.cnblogs.com/yingkong1987/archive/ ...

  8. error launching remote program failed to get the task for process

    Error  Starting executable: error launching remote program failed to get the task for process 715 这个 ...

  9. 【docker】关于docker中挂载的解释

    现在有这么一个命令: docker run -p 33061:3306 --name mysql --restart=always -e MYSQL_ROOT_PASSWORD=pisen -v /e ...

  10. NGUI 3.5教程(一)安装NGUI 3.5.8

    写在前面: 网上找的NGUI教程,都是基于2.x版本号的.为了能配合教程学着做,我也是下载了各种NGUI 2.x版本号.可是在导入的时候,或多或少都报错(我用的Unity 的版本号是4.3.2).无奈 ...