一、为什么要使用Mock工具
在做单元测试的时候,我们会发现我们要测试的方法会引用很多外部依赖的对象,比如:(发送邮件,网络通讯,远程服务, 文件系统等等)。 而我们没法控制这些外部依赖的对象,为了解决这个问题,我们就需要用到Mock工具来模拟这些外部依赖的对象,来完成单元测试。
二、为什么要使用PowerMock
现如今比较流行的Mock工具如EasyMock 、Mockito等都有一个共同的缺点:不能mock静态、final、私有方法等。而PowerMock能够完美的弥补以上三个Mock工具的不足。
三、PowerMock简介
PowerMock是一个扩展了其它如EasyMock等mock框架的、功能更加强大的框架。PowerMock使用一个自定义类加载器和字节码操作来模拟静态方法,构造函数,final类和方法,私有方法,去除静态初始化器等等。通过使用自定义的类加载器,简化采用的IDE或持续集成服务器不需要做任何改变。熟悉PowerMock支持的mock框架的开发人员会发现PowerMock很容易使用,因为对于静态方法和构造器来说,整个的期望API是一样的。PowerMock旨在用少量的方法和注解扩展现有的API来实现额外的功能。目前PowerMock支持EasyMock和Mockito。
四、PowerMock入门
PowerMock有两个重要的注解:
–@RunWith(PowerMockRunner.class)
–@PrepareForTest( { YourClassWithEgStaticMethod.class })
如果你的测试用例里没有使用注解@PrepareForTest,那么可以不用加注解@RunWith(PowerMockRunner.class),反之亦然。当你需要使用PowerMock强大功能(Mock静态、final、私有方法等)的时候,就需要加注解@PrepareForTest。
五、PowerMock基本用法
(1) 普通Mock: Mock参数传递的对象
测试目标代码:
1 |
public boolean callArgumentInstance(File file) { |
测试用例代码:
02 |
public void testCallArgumentInstance() { |
04 |
File file = PowerMockito.mock(File.class); |
06 |
ClassUnderTest underTest = new ClassUnderTest(); |
08 |
PowerMockito.when(file.exists()).thenReturn(true); |
10 |
Assert.assertTrue(underTest.callArgumentInstance(file)); |
说明:普通Mock不需要加@RunWith和@PrepareForTest注解。
(2) Mock方法内部new出来的对象
测试目标代码:
01 |
public class ClassUnderTest { |
03 |
public boolean callInternalInstance(String path) { |
05 |
File file = new File(path); |
测试用例代码:
01 |
@RunWith(PowerMockRunner.class) |
02 |
public class TestClassUnderTest { |
05 |
@PrepareForTest(ClassUnderTest.class) |
06 |
public void testCallInternalInstance() throws Exception { |
08 |
File file = PowerMockito.mock(File.class); |
10 |
ClassUnderTest underTest = new ClassUnderTest(); |
12 |
PowerMockito.whenNew(File.class).withArguments("bbb").thenReturn(file); |
14 |
PowerMockito.when(file.exists()).thenReturn(true); |
16 |
Assert.assertTrue(underTest.callInternalInstance("bbb")); |
说明:当使用PowerMockito.whenNew方法时,必须加注解@PrepareForTest和@RunWith。注解@PrepareForTest里写的类是需要mock的new对象代码所在的类。
(3) Mock普通对象的final方法
测试目标代码:
1 |
public class ClassUnderTest { |
3 |
public boolean callFinalMethod(ClassDependency refer) { |
5 |
return refer.isAlive(); |
01 |
public class ClassDependency { |
03 |
public final boolean isAlive() { |
测试用例代码:
01 |
@RunWith(PowerMockRunner.class) |
02 |
public class TestClassUnderTest { |
05 |
@PrepareForTest(ClassDependency.class) |
06 |
public void testCallFinalMethod() { |
08 |
ClassDependency depencency = PowerMockito.mock(ClassDependency.class); |
10 |
ClassUnderTest underTest = new ClassUnderTest(); |
12 |
PowerMockito.when(depencency.isAlive()).thenReturn(true); |
14 |
Assert.assertTrue(underTest.callFinalMethod(depencency)); |
说明: 当需要mock final方法的时候,必须加注解@PrepareForTest和@RunWith。注解@PrepareForTest里写的类是final方法所在的类。
(4) Mock普通类的静态方法
测试目标代码:
1 |
public class ClassUnderTest { |
3 |
public boolean callStaticMethod() { |
5 |
return ClassDependency.isExist(); |
01 |
public class ClassDependency { |
03 |
public static boolean isExist() { |
测试用例代码:
01 |
@RunWith(PowerMockRunner.class) |
02 |
public class TestClassUnderTest { |
05 |
@PrepareForTest(ClassDependency.class) |
06 |
public void testCallStaticMethod() { |
08 |
ClassUnderTest underTest = new ClassUnderTest(); |
10 |
PowerMockito.mockStatic(ClassDependency.class); |
12 |
PowerMockito.when(ClassDependency.isExist()).thenReturn(true); |
14 |
Assert.assertTrue(underTest.callStaticMethod()); |
说明:当需要mock静态方法的时候,必须加注解@PrepareForTest和@RunWith。注解@PrepareForTest里写的类是静态方法所在的类。
(5) Mock 私有方法
测试目标代码:
01 |
public class ClassUnderTest { |
03 |
public boolean callPrivateMethod() { |
09 |
private boolean isExist() { |
测试用例代码:
01 |
@RunWith(PowerMockRunner.class) |
02 |
public class TestClassUnderTest { |
05 |
@PrepareForTest(ClassUnderTest.class) |
06 |
public void testCallPrivateMethod() throws Exception { |
08 |
ClassUnderTest underTest = PowerMockito.mock(ClassUnderTest.class); |
10 |
PowerMockito.when(underTest.callPrivateMethod()).thenCallRealMethod(); |
12 |
PowerMockito.when(underTest, "isExist").thenReturn(true); |
14 |
Assert.assertTrue(underTest.callPrivateMethod()); |
说明:和Mock普通方法一样,只是需要加注解@PrepareForTest(ClassUnderTest.class),注解里写的类是私有方法所在的类。
(6) Mock系统类的静态和final方法
测试目标代码:
01 |
public class ClassUnderTest { |
03 |
public boolean callSystemFinalMethod(String str) { |
09 |
public String callSystemStaticMethod(String str) { |
11 |
return System.getProperty(str); |
测试用例代码:
01 |
@RunWith(PowerMockRunner.class) |
02 |
public class TestClassUnderTest { |
05 |
@PrepareForTest(ClassUnderTest.class) |
06 |
public void testCallSystemStaticMethod() { |
08 |
ClassUnderTest underTest = new ClassUnderTest(); |
10 |
PowerMockito.mockStatic(System.class); |
12 |
PowerMockito.when(System.getProperty("aaa")).thenReturn("bbb"); |
14 |
Assert.assertEquals("bbb", underTest.callJDKStaticMethod("aaa")); |
说明:和Mock普通对象的静态方法、final方法一样,只不过注解@PrepareForTest里写的类不一样 ,注解里写的类是需要调用系统方法所在的类。
六 、无所不能的PowerMock
(1) 验证静态方法:
PowerMockito.verifyStatic();
Static.firstStaticMethod(param);
(2) 扩展验证:
PowerMockito.verifyStatic(Mockito.times(2)); // 被调用2次 Static.thirdStaticMethod(Mockito.anyInt()); // 以任何整数值被调用
(3) 更多的Mock方法
http://code.google.com/p/powermock/wiki/MockitoUsage13
七、PowerMock简单实现原理
• 当某个测试方法被注解@PrepareForTest标注以后,在运行测试用例时,会创建一个新的org.powermock.core.classloader.MockClassLoader实例,然后加载该测试用例使用到的类(系统类除外)。
• PowerMock会根据你的mock要求,去修改写在注解@PrepareForTest里的class文件(当前测试类会自动加入注解中),以满足特殊的mock需求。例如:去除final方法的final标识,在静态方法的最前面加入自己的虚拟实现等。
• 如果需要mock的是系统类的final方法和静态方法,PowerMock不会直接修改系统类的class文件,而是修改调用系统类的class文件,以满足mock需求。
powmock的maven依赖:
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.6.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.6.1</version>
<scope>test</scope>
</dependency>
1 |
public boolean callArgumentInstance(File file) { |
测试用例代码:
02 |
public void testCallArgumentInstance() { |
04 |
File file = PowerMockito.mock(File.class); |
06 |
ClassUnderTest underTest = new ClassUnderTest(); |
08 |
PowerMockito.when(file.exists()).thenReturn(true); |
10 |
Assert.assertTrue(underTest.callArgumentInstance(file)); |
说明:普通Mock不需要加@RunWith和@PrepareForTest注解。
(2) Mock方法内部new出来的对象
测试目标代码:
01 |
public class ClassUnderTest { |
03 |
public boolean callInternalInstance(String path) { |
05 |
File file = new File(path); |
测试用例代码:
01 |
@RunWith(PowerMockRunner.class) |
02 |
public class TestClassUnderTest { |
05 |
@PrepareForTest(ClassUnderTest.class) |
06 |
public void testCallInternalInstance() throws Exception { |
08 |
File file = PowerMockito.mock(File.class); |
10 |
ClassUnderTest underTest = new ClassUnderTest(); |
12 |
PowerMockito.whenNew(File.class).withArguments("bbb").thenReturn(file); |
14 |
PowerMockito.when(file.exists()).thenReturn(true); |
16 |
Assert.assertTrue(underTest.callInternalInstance("bbb")); |
说明:当使用PowerMockito.whenNew方法时,必须加注解@PrepareForTest和@RunWith。注解@PrepareForTest里写的类是需要mock的new对象代码所在的类。
(3) Mock普通对象的final方法
测试目标代码:
1 |
public class ClassUnderTest { |
3 |
public boolean callFinalMethod(ClassDependency refer) { |
5 |
return refer.isAlive(); |
01 |
public class ClassDependency { |
03 |
public final boolean isAlive() { |
测试用例代码:
01 |
@RunWith(PowerMockRunner.class) |
02 |
public class TestClassUnderTest { |
05 |
@PrepareForTest(ClassDependency.class) |
06 |
public void testCallFinalMethod() { |
08 |
ClassDependency depencency = PowerMockito.mock(ClassDependency.class); |
10 |
ClassUnderTest underTest = new ClassUnderTest(); |
12 |
PowerMockito.when(depencency.isAlive()).thenReturn(true); |
14 |
Assert.assertTrue(underTest.callFinalMethod(depencency)); |
说明: 当需要mock final方法的时候,必须加注解@PrepareForTest和@RunWith。注解@PrepareForTest里写的类是final方法所在的类。
(4) Mock普通类的静态方法
测试目标代码:
1 |
public class ClassUnderTest { |
3 |
public boolean callStaticMethod() { |
5 |
return ClassDependency.isExist(); |
01 |
public class ClassDependency { |
03 |
public static boolean isExist() { |
测试用例代码:
01 |
@RunWith(PowerMockRunner.class) |
02 |
public class TestClassUnderTest { |
05 |
@PrepareForTest(ClassDependency.class) |
06 |
public void testCallStaticMethod() { |
08 |
ClassUnderTest underTest = new ClassUnderTest(); |
10 |
PowerMockito.mockStatic(ClassDependency.class); |
12 |
PowerMockito.when(ClassDependency.isExist()).thenReturn(true); |
14 |
Assert.assertTrue(underTest.callStaticMethod()); |
说明:当需要mock静态方法的时候,必须加注解@PrepareForTest和@RunWith。注解@PrepareForTest里写的类是静态方法所在的类。
(5) Mock 私有方法
测试目标代码:
01 |
public class ClassUnderTest { |
03 |
public boolean callPrivateMethod() { |
09 |
private boolean isExist() { |
测试用例代码:
01 |
@RunWith(PowerMockRunner.class) |
02 |
public class TestClassUnderTest { |
05 |
@PrepareForTest(ClassUnderTest.class) |
06 |
public void testCallPrivateMethod() throws Exception { |
08 |
ClassUnderTest underTest = PowerMockito.mock(ClassUnderTest.class); |
10 |
PowerMockito.when(underTest.callPrivateMethod()).thenCallRealMethod(); |
12 |
PowerMockito.when(underTest, "isExist").thenReturn(true); |
14 |
Assert.assertTrue(underTest.callPrivateMethod()); |
说明:和Mock普通方法一样,只是需要加注解@PrepareForTest(ClassUnderTest.class),注解里写的类是私有方法所在的类。
(6) Mock系统类的静态和final方法
测试目标代码:
01 |
public class ClassUnderTest { |
03 |
public boolean callSystemFinalMethod(String str) { |
09 |
public String callSystemStaticMethod(String str) { |
11 |
return System.getProperty(str); |
测试用例代码:
01 |
@RunWith(PowerMockRunner.class) |
02 |
public class TestClassUnderTest { |
05 |
@PrepareForTest(ClassUnderTest.class) |
06 |
public void testCallSystemStaticMethod() { |
08 |
ClassUnderTest underTest = new ClassUnderTest(); |
10 |
PowerMockito.mockStatic(System.class); |
12 |
PowerMockito.when(System.getProperty("aaa")).thenReturn("bbb"); |
14 |
Assert.assertEquals("bbb", underTest.callJDKStaticMethod("aaa")); |
说明:和Mock普通对象的静态方法、final方法一样,只不过注解@PrepareForTest里写的类不一样 ,注解里写的类是需要调用系统方法所在的类。
六 、无所不能的PowerMock
(1) 验证静态方法:
PowerMockito.verifyStatic();
Static.firstStaticMethod(param);
(2) 扩展验证:
PowerMockito.verifyStatic(Mockito.times(2)); // 被调用2次 Static.thirdStaticMethod(Mockito.anyInt()); // 以任何整数值被调用
(3) 更多的Mock方法
http://code.google.com/p/powermock/wiki/MockitoUsage13
七、PowerMock简单实现原理
• 当某个测试方法被注解@PrepareForTest标注以后,在运行测试用例时,会创建一个新的org.powermock.core.classloader.MockClassLoader实例,然后加载该测试用例使用到的类(系统类除外)。
• PowerMock会根据你的mock要求,去修改写在注解@PrepareForTest里的class文件(当前测试类会自动加入注解中),以满足特殊的mock需求。例如:去除final方法的final标识,在静态方法的最前面加入自己的虚拟实现等。
• 如果需要mock的是系统类的final方法和静态方法,PowerMock不会直接修改系统类的class文件,而是修改调用系统类的class文件,以满足mock需求。
- mockito模拟静态方法
这里要用到使用powerMock 注意点: 1 @RunWith(PowerMockRunner.class) 2 PowerMockito.mockStatic(StaticTest.class); ...
- js模拟静态方法
//模拟静态 var Animal = function(name){ this.name = name; Animal.instanceCounter ++; }; Animal.instanceC ...
- 使用MRUnit,Mockito和PowerMock进行Hadoop MapReduce作业的单元测试
0.preliminary 环境搭建 Setup development environment Download the latest version of MRUnit jar from Apac ...
- 使用PowerMockito和Mockito进行模拟测试,包括静态方法测试,私有方法测试等,以及方法执行的坑或者模拟不成功解决
依赖:这个很重要,不同版本用法也有点区别: <dependency> <groupId>org.mockito</groupId> <artifactId&g ...
- Mockito为什么不能mock静态方法
因为Mockito使用继承的方式实现mock的,用CGLIB生成mock对象代替真实的对象进行执行,为了mock实例的方法,你可以在subclass中覆盖它,而static方法是不能被子类覆盖的,所以 ...
- mock测试框架Mockito
无论是敏捷开发.持续交付,还是测试驱动开发(TDD)都把单元测试作为实现的基石.随着这些先进的编程开发模式日益深入人心,单元测试如今显得越来越重要了.在敏捷开发.持续交付中要求单元测试一定要快(不能访 ...
- 单元测试mock之mockito使用
先来一个简单的例子来感受一下 外部接口类:TestService.java package com.yzl.mock; /** * 测试用服务 * * @author yangzhilong */ p ...
- 使用 Mockito 单元测试 – 教程
tanyuanji@126.com 版本历史 - - - - 使用 Mockito 进行测试 该教程主要讲解 Mockito 框架在Eclipse IDE 中的使用 目录 tanyuanji@12 ...
- 使用Mockito进行单元测试【1】——mock and verify[转]
本文转自:http://qiuguo0205.iteye.com/blog/1443344 1. 为什么使用Mockito来进行单元测试? 回答这个问题需要回答两个方面,第一个是为什么使用mock?m ...
随机推荐
- python中的常用模块(2)
在自动化测试中,经常需要查找操作文件,比如说查找配置文件(从而读取配置文件的信息),查找测试报告(从而发送测试报告邮件), 经常要对大量文件和大量路径进行操作,这就依赖于os模块. 1.当前路径及路径 ...
- 科学计算三维可视化---Mlab基础(数据可视化)
推文:科学计算三维可视化---TVTK库可视化实例 使用相关函数:科学计算三维可视化---Mlab基础(管线控制函数) 一:mlab.pipeline中标量数据可视化 通过持续实例,来感受mlab对数 ...
- 解决错误:Couldn't open file /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
在使用yum install的时候,偶尔会碰见这样的错误:Couldn’t open file /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7 这是因为在你的 /etc/yum ...
- 51nod 1103 N的倍数 (鸽巢原理)
1103 N的倍数 题目来源: Ural 1302 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注 一个长度为N的数组A,从A中选出若干个数,使得这 ...
- noi题库(noi.openjudge.cn) 3.9数据结构之C++STL T1——T2
T1 1806:词典 描述 你旅游到了一个国外的城市.那里的人们说的外国语言你不能理解.不过幸运的是,你有一本词典可以帮助你. 输入首先输入一个词典,词典中包含不超过100000个词条,每个词条占据一 ...
- Docker中执行Shell出现乱码
问题描述 最近遇到一个问题: 执行命令 docker exec f4af9b sh -c 'bash /tmp/build.sh' 命令在docker中执行shell,会出现中文乱码的问题.但是在do ...
- Python入门系列教程(二)字符串
字符串 1.字符串输出 name = 'xiaoming' print("姓名:%s"%name) 2.字符串输入 userName = raw_input('请输入用户名:') ...
- 是否使用TDD(测试驱动开发)进行UI开发
问题 StackOverflow上有一则是否使用TDD(测试驱动开发)进行UI开发 的提问. _JacobE_问: 对于是否使用TDD进行开发UI这件事,我想了很久,但难以决定.我想听听你们的意见. ...
- Win7下VS2010不能链接问题
装了2012准备学VC++窗体开发,然后发现手边只有VS2010的教程,于是卸掉VS2012改装VS2010,结果发现不管写啥,链接时都报错“error Link1123 转到coff期间失败”. 于 ...
- 个人对java中对象锁与类锁的一些理解与实例
一 什么是对象锁 对象锁也叫方法锁,是针对一个对象实例的,它只在该对象的某个内存位置声明一个标识该对象是否拥有锁,所有它只会锁住当前的对象,而并不会对其他对象实例的锁产生任何影响,不同对象访问同一个 ...