1、PowerMock概述java
现现在比较流行的Mock工具如jMock,EasyMock,Mockito等都有一个共同的缺点:不能mock静态、final、私有方法等。而PowerMock可以完美的弥补以上三个Mock工具的不足。服务器
PowerMock是一个扩展了其它如EasyMock等mock框架的、功能更增强大的框架。PowerMock使用一个自定义类加载器和字节码操做来模拟静态方法,构造函数,final类和方法,私有方法,去除静态初始化器等等。经过使用自定义的类加载器,简化采用的IDE或持续集成服务器不须要作任何改变。熟悉PowerMock支持的mock框架的开发人员会发现PowerMock很容易使用,由于对于静态方法和构造器来讲,整个的指望API是同样的。PowerMock旨在用少许的方法和注解扩展示有的API来实现额外的功能。目前PowerMock支持EasyMock和Mockito。框架
二、PowerMock入门 函数
PowerMock有两个重要的注解:工具
–@RunWith(PowerMockRunner.class)测试
–@PrepareForTest( { YourClassWithEgStaticMethod.class })google
若是你的测试用例里没有使用注解@PrepareForTest,那么能够不用加注解@RunWith(PowerMockRunner.class),反之亦然。当你须要使用PowerMock强大功能(Mock静态、final、私有方法等)的时候,就须要加注解@PrepareForTest。spa
三、PowerMock基本用法code
(1) 普通Mock: Mock参数传递的对象对象
//测试目标代码: public boolean callArgumentInstance(File file) { return file.exists(); } //测试用例代码: @Test public void testCallArgumentInstance() { File file = PowerMockito.mock(File.class); Source underTest = new Source(); PowerMockito.when(file.exists()).thenReturn(true); Assert.assertTrue(underTest.callArgumentInstance(file)); }
说明:普通Mock不须要加@RunWith和@PrepareForTest注解。
(2) Mock方法内部new出来的对象
//测试目标代码: public boolean callInternalInstance(String path) { File file = new File(path); return file.exists(); } //测试用例代码: @Test @PrepareForTest(Source.class) public void testCallInternalInstance() throws Exception { File file = PowerMockito.mock(File.class); Source underTest = new Source(); PowerMockito.whenNew(File.class).withArguments("bbb").thenReturn(file); PowerMockito.when(file.exists()).thenReturn(true); Assert.assertTrue(underTest.callInternalInstance("bbb")); }
说明:当使用PowerMockito.whenNew方法时,必须加注解@PrepareForTest和@RunWith。注解@PrepareForTest里写的类是须要mock的new对象代码所在的类。
(3) Mock普通对象的final方法
//测试目标代码: public class Source { public boolean callFinalMethod(SourceDepend refer) { return refer.isAlive(); } } public class SourceDepend { public final boolean isAlive() { return false; } } //测试用例代码: @Test @PrepareForTest(SourceDepend.class) public void testCallFinalMethod() { SourceDepend depencency = PowerMockito.mock(SourceDepend.class); Source underTest = new Source(); PowerMockito.when(depencency.isAlive()).thenReturn(true); Assert.assertTrue(underTest.callFinalMethod(depencency)); }
说明: 当须要mock final方法的时候,必须加注解@PrepareForTest和@RunWith。注解@PrepareForTest里写的类是final方法所在的类。
(4) Mock普通类的静态方法
//测试目标代码: public boolean callStaticMethod() { return SourceDepend.isExist(); } public static boolean isExist() { return false; } //测试用例代码: @Test @PrepareForTest(SourceDepend.class) public void testCallStaticMethod() { Source underTest = new Source(); PowerMockito.mockStatic(SourceDepend.class); PowerMockito.when(SourceDepend.isExist()).thenReturn(true); Assert.assertTrue(underTest.callStaticMethod()); }
说明:当须要mock静态方法的时候,必须加注解@PrepareForTest和@RunWith。注解@PrepareForTest里写的类是静态方法所在的类。
(5) Mock 私有方法
//测试目标代码: public boolean callPrivateMethod() { return isExist(); } private boolean isExist() { return false; } //测试用例代码: @Test @PrepareForTest(Source.class) public void testCallPrivateMethod() throws Exception { Source underTest = PowerMockito.mock(Source.class); PowerMockito.when(underTest.callPrivateMethod()).thenCallRealMethod(); PowerMockito.when(underTest, "isExist").thenReturn(true); Assert.assertTrue(underTest.callPrivateMethod()); }
说明:和Mock普通方法同样,只是须要加注解@PrepareForTest(ClassUnderTest.class),注解里写的类是私有方法所在的类。
(6) Mock系统类的静态和final方法
//测试目标代码: public boolean callSystemFinalMethod(String str) { return str.isEmpty(); } public String callSystemStaticMethod(String str) { return System.getProperty(str); } //测试用例代码: @Test @PrepareForTest(Source.class) public void testCallSystemStaticMethod() { Source underTest = new Source(); PowerMockito.mockStatic(System.class); PowerMockito.when(System.getProperty("aaa")).thenReturn("bbb"); Assert.assertEquals("bbb", underTest.callSystemStaticMethod("aaa")); }
说明:和Mock普通对象的静态方法、final方法同样,只不过注解@PrepareForTest里写的类不同 ,注解里写的类是须要调用系统方法所在的类。
4、无所不能的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
5、PowerMock简单实现原理
当某个测试方法被注解@PrepareForTest标注之后,在运行测试用例时,会建立一个新的org.powermock.core.classloader.MockClassLoader实例,而后加载该测试用例使用到的类(系统类除外)。
PowerMock会根据你的mock要求,去修改写在注解@PrepareForTest里的class文件(当前测试类会自动加入注解中),以知足特殊的mock需求。例如:去除final方法的final标识,在静态方法的最前面加入本身的虚拟实现等。
若是须要mock的是系统类的final方法和静态方法,PowerMock不会直接修改系统类的class文件,而是修改调用系统类的class文件,以知足mock需求。