一、为什么要使用Mock工具
在做单元测试的时候,我们会发现我们要测试的方法会引用很多外部依赖的对象,比如:(发送邮件,网络通讯,远程服务, 文件系统等等)。 而我们没法控制这些外部依赖的对象,为了解决这个问题,我们就需要用到Mock工具来模拟这些外部依赖的对象,来完成单元测试。
二、为什么要使用PowerMock
现如今比较流行的Mock工具如jMock 、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需求。
相关推荐
什么时候需要Mock对象 什么是EasyMock EasyMock的优点和缺点 运行EasyMock需要的资源 EasyMock的基本使用步骤 什么是PowerMock 运行PowerMock需要的资源 PowerMock的扩展功能
全面介绍PowerMock的使用,帮助初学者彻底了解PowerMock的使用,提高代码的健壮性
介绍即使在 2011 年,仍有许多遗留项目需要 Java 1.4(特别是在大型保守企业中),而且这些项目中的大多数在设计时都没有考虑到可测试性,因此它们倾向于使用和滥用静态(有时是最终)方法。 这些方法不容易用 ...
我会介绍1.使用MRUnit来编写HadoopMapReduce应用程序的JUnit测试2.使用PowerMock和Mockito模拟静态方法3.模拟其他类型中的业务逻辑(译注:也就是编写测试驱动模块)4.查看模拟的业务逻辑是否被调用(译注:测试驱动...
之前《有效使用Mock编写java单元测试》一文中层介绍过使用EasyMock和PowerMock来编写java单元测试,今天介绍一个更加强大的工具——JMockit。引用单元测试中mock的使用及mock神器jmockit实践中的java单元测试中各种...
主要介绍了MyBatis-Plus 如何单元测试的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
python 2.7 中文教程及自动化测试介绍 使用Python学习selenium测试工具 性能测试艺术 Java单元测试之模拟利器-使用PowerMock进行Mock测试 网络工具ATC 主页 ...
《Android测试开发--基于Java编程语言技术体系》 基于Java编程语言,帮你...PowerMock框架 Android环境搭建 Android SDK Android Studio Android项目介绍 构建简单的用户界面 启动另一个 Activity Android测试支持库
令人欣慰的是开源社区各种优秀的Mock框架让单元测试不再复杂,本文简单介绍EasyMock,PowerMock等的基本常用用法。Mock说白了就是打桩(Stub)或则模拟,当你调用一个不好在测试中创建的对象时,Mock框架为你模拟一...
本文仅介绍本地JVM测试 本地jvm的单元测试 这种方式运行速度快,对运行环境没有特殊要求,可以很方便的做自动化测试,是单元测试首选的方法 Instrumentation测试 Instrumentation测试需要运行在Android环境下,...