EasyMock使用总结
最重要的事说在前面:遇到一个你不熟悉的知识,一定要去官网仔仔细细的看官方文档!一定要仔仔细细!一定要!(尔康鼻孔脸。。)
正篇:
一.使用
首先,当然是添加依赖,有人用maven,有人用ant或者mantis,每个人不一样,嗯,添加依赖。然后如果你需要mock一个类而不是接口的话,需要添加objenesis的依赖和jar包。(easymock3.0及以上适用)
二.你可能得事先了解的一些api
1.createControl,如果在你需要在相对复杂的测试用例中使用多个 Mock 对象,EasyMock 提供了另外一种生成和管理 Mock 对象的机制:
@Before
protected void setUp() throws Exception {
super.setUp();
IMocksControl control = EasyMock.createControl();
servlet = new GetIdeationsServlet();
service = RollerFactory.getRoller().getUserManager();
request = control.createMock(HttpServletRequest.class);
response = control.createMock(HttpServletResponse.class);
session = control.createMock(HttpSession.class);
servletConfig = control.createMock(ServletConfig.class);
servletContext = control.createMock(ServletContext.class);
rollersession = control.createMock(RollerSession.class); servlet.init(servletConfig); }
IMocksControl
的对象,该对象能创建并管理多个 Mock 对象。
2.createMock, 就像上面代码所示,你创建了管理对象IMockControl,就可以通过它去mock你要mock的对象。
3.有了createControl和createMock,也会有createNiceControl,createStrictControl和createNiceMock和createStrictMock。那么它们之间究竟有啥区别呢?官方api是这么说的
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA28AAABPCAIAAADz6WoAAAASw0lEQVR4nO2dvW7kuBJG550uFtg3aMDZLrC5MInDvaHGoUPPAoYmnknvQtmEztWYaJ+l/Qh7A/1VkVUltlr9Y88RTmCrJapYLJKfSEr68C8bGxsbGxsbGxvb2u3DtQ1gY2NjY2NjY2N7w9uHw+EVAAAAAGAdqEkAAAAAWA9qEgAAAADWg5oEAAAAgPWgJgEAAABgPahJAAAAAFgPahIAAAAA1oOaBAAAAID1oCYBAAAAYD2oSQAAAABYD2oSAAAAANaDmgQAAACA9bwTNdk+Pd7dP3789s/VLdmaf758ery7H3Az2H3+/dc//6d27j//8Z/f/9pf234AAAB451xLTQ4i6eF7ycEvD72ceno5H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qT03qBQylY5Pr1OT8K5oSAABgHeef6dZztcM8rzhg8ZnuZHlf2aMS157p9tPsp4AzPwQz3aZqPGam2xRJ2890z/+qZbXHznQnhp11prvUpQAAAOBiPIWjJi71QxJr1aQcJsxGgLJTuqYSF9IjVdkzNKVP4UyXMJ7CiVfOJQ+aHL2CUB2mn01xHhPxn8IR+Sp4CqdfIqlFkshvOkm9hZoc8pGvTzCDyn4KR5V4W9sz3WOQJIPQ6ubH9NshWTepjhnyFXsDAAAAJMe9IWh5NtAUZ9Pavf7FMlkK0+9a/4llgvJ4+y0wEmPZZfiGoGRdo5mpIbWmHq87p1g1Xf4FoL3KQSdtmJYtSiePg3r6lMQ/iZ12psT+0XU6Aausp1f+5H5Qli7k+tWZMT/yDUHyfUN1rc8SwaKcGMSGOkzlwPEhahIAAKAcvtN9IzDNCgAAAG8S1OSNgJoEAACANwlq8hbIZmwBAAAA3gioSQAAAABYD2oSAAAAANaDmgQAAACA9aAmAQAAAGA9qEkAAAAAWA9qEgAAAADWg5oEAAAAgPV8+JeNjY2NjY2NjY1t7YaaZGNjY2NjY2NjW7/9HxQx9oUomJ3bAAAAAElFTkSuQmCC" alt="" />
懒,其实就是调用顺序检测不检测的问题。什么是调用顺序?什么是调用顺序检测?http://skydream.iteye.com/blog/829333
4.http://www.cnblogs.com/alipayhutu/archive/2012/05/21/2512363.html其他的问题这里总结的已经非常好了!
5.感谢以上两位博主。
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 方法是单元测试中常见的一种技术,它的主要作用是模拟一 ...
随机推荐
- git revert回退时提示One or more files are in a conflicted state
解决代码冲突 如果commit时出现“You have to update your work copy first.”红色警告,说明版本库中的此文件已经被其他人修改了. 请先点“ok”按钮退出.执行 ...
- Package rdkafka was not found in the pkg-config search path.
问题 在使用confluent-kafka-go 时遇到如下问题: $ go build t.go # pkg-config --cflags rdkafka Package rdkafka was ...
- 利用Python玩微信跳一跳
创建python项目jump_weixin,新建python程序jump.py 需要4个辅助文件[adb.exe,AdbWinApi.dll,AdbWinUsbApi.dll,fastboot.exe ...
- 为什么要使用MONO
今天中午我收到一个Email,是关于以前写的一个MONO文章的疑问,我对此做了一些解释,希望与有相同问题的朋友一起分享一下,邮件内容如下: 我在网上找到了您写的一篇关于Mono的“在windows下使 ...
- Spark1.6 Idea下远程调试
使用的开发工具:scala2.10.4.Idea16.JDK8 1.导入依赖的包和源码 在下载Spark中到的导入spark的相关依赖了包和其源码.zip,相关源码的下载地址:https://gith ...
- CefSharp v62修改,支持.net4.0
吐槽一下,博客园久了没有上,账号没了,重新申请一个. cesharp v62版本,内核采用最新的Cef 62,支持最新的Grid布局. 由于官方的cefsharp 采用.net4.5.2开发.怎么办怎 ...
- DevExpress 组件
最近看到 伍华聪 的博客里, DevExpress 组件那个效果很好看,特别是 LookUpEdit GridLookUpEdit 这两个控件,完美改善了 WinForm 里的 ComboBox 今天 ...
- dubbo无法创建线程问题
OutOfMemoryError: unable to create new native thread 决定当前用户程序能够创建多少线程由2个因素决定 1. 用户环境允许的线程数 cat /etc/ ...
- python 模块被引用多次但是里面的全局表达式总共只会执行一次
python 模块被引用多次但是里面的全局表达式总共只会执行一次
- javascript创建对象之构造函数模式(二)
对上一章节的工厂模式进行代码重写 function Human(name, sex) { this.name = name; this.sex = sex; this.say = function ( ...