1、为何要使用Mock工具java
在作单元测试的时候,咱们会发现咱们要测试的方法会引用不少外部依赖的对象,好比:(发送邮件,网络通信,远程服务, 文件系统等等)。 而咱们无法控制这些外部依赖的对象,为了解决这个问题,咱们就须要用到Mock工具来模拟这些外部依赖的对象,来完成单元测试。api
2、为何要使用PowerMock服务器
现现在比较流行的Mock工具如jMock 、EasyMock 、Mockito等都有一个共同的缺点:不能mock静态、final、私有方法等。而PowerMock可以完美的弥补以上三个Mock工具的不足。网络
3、PowerMock简介框架
PowerMock是一个扩展了其它如EasyMock等mock框架的、功能更增强大的框架。PowerMock使用一个自定义类加载器和字节码操做来模拟静态方法,构造函数,final类和方法,私有方法,去除静态初始化器等等。经过使用自定义的类加载器,简化采用的IDE或持续集成服务器不须要作任何改变。熟悉PowerMock支持的mock框架的开发人员会发现PowerMock很容易使用,由于对于静态方法和构造器来讲,整个的指望API是同样的。PowerMock旨在用少许的方法和注解扩展示有的API来实现额外的功能。目前PowerMock支持EasyMock和Mockito。函数
4、PowerMock入门 工具
PowerMock有两个重要的注解:单元测试
–@RunWith(PowerMockRunner.class)测试
–@PrepareForTest( { YourClassWithEgStaticMethod.class })google
若是你的测试用例里没有使用注解@PrepareForTest,那么能够不用加注解@RunWith(PowerMockRunner.class),反之亦然。当你须要使用PowerMock强大功能(Mock静态、final、私有方法等)的时候,就须要加注解@PrepareForTest。
5、PowerMock基本用法
(1) 普通Mock: Mock参数传递的对象
测试目标代码:
1 public class FlySunDemo { 2 public boolean callArgumentInstance(File file) { 3 return file.exists(); 4 } 5 }
测试用例代码:
1 import java.io.File; 2 import org.junit.Assert; 3 import org.junit.Test; 4 import org.powermock.api.mockito.PowerMockito; 5 6 public class FlySunMockTest { 7 @Test 8 public void testCallArgumentInstance(){ 9 //mock出入参File对象 10 File file = PowerMockito.mock(File.class); 11 FlySunDemo demo = new FlySunDemo(); 12 PowerMockito.when(file.exists()).thenReturn(true); 13 Assert.assertTrue(demo.callArgumentInstance(file)); 14 } 15 }
说明:普通Mock不须要加@RunWith和@PrepareForTest注解。
(2) Mock方法内部new出来的对象
测试目标代码:
1 public class FlySunDemo { 2 public boolean callArgumentInstance(String path) { 3 File file = new File(path); 4 return file.exists(); 5 } 6 }
测试用例代码:
1 import java.io.File; 2 import org.junit.Assert; 3 import org.junit.Test; 4 import org.junit.runner.RunWith; 5 import org.powermock.api.mockito.PowerMockito; 6 import org.powermock.core.classloader.annotations.PrepareForTest; 7 import org.powermock.modules.junit4.PowerMockRunner; 8 @RunWith(PowerMockRunner.class) 9 public class FlySunMockTest { 10 @Test 11 @PrepareForTest(FlySunDemo.class) 12 public void testCallArgumentInstance(){ 13 File file = PowerMockito.mock(File.class); 14 try { 15 PowerMockito.whenNew(File.class).withArguments("bbb").thenReturn(file); 16 FlySunDemo demo = new FlySunDemo(); 17 PowerMockito.when(file.exists()).thenReturn(true); 18 Assert.assertTrue(demo.callArgumentInstance("bbb")); 19 } catch (Exception e) { 20 e.printStackTrace(); 21 } 22 } 23 }
说明:当使用PowerMockito.whenNew方法时,必须加注解@PrepareForTest和@RunWith。注解@PrepareForTest里写的类是须要mock的new对象代码所在的类。
(3) Mock普通对象的final方法
测试目标代码:
1 public class ClassDependency { 2 public final boolean isAlive() { 3 // do something 4 return false; 5 } 6 }
1 public class FlySunDemo { 2 public boolean callFinalMethod(ClassDependency refer) { 3 return refer.isAlive(); 4 } 5 }
测试用例代码:
1 import java.io.File; 2 import org.junit.Assert; 3 import org.junit.Test; 4 import org.junit.runner.RunWith; 5 import org.powermock.api.mockito.PowerMockito; 6 import org.powermock.core.classloader.annotations.PrepareForTest; 7 import org.powermock.modules.junit4.PowerMockRunner; 8 9 @RunWith(PowerMockRunner.class) 10 public class FlySunMockTest { 11 @Test 12 @PrepareForTest(ClassDependency.class) 13 public void testCallFinalMethod() { 14 ClassDependency refer = PowerMockito.mock(ClassDependency.class); 15 PowerMockito.when(refer.isAlive()).thenReturn(true); 16 FlySunDemo demo = new FlySunDemo(); 17 Assert.assertTrue(demo.callFinalMethod(refer)); 18 } 19 }
说明: 当须要mock final方法的时候,必须加注解@PrepareForTest和@RunWith。注解@PrepareForTest里写的类是final方法所在的类。
(4) Mock普通类的静态方法
测试目标代码:
1 public class ClassDependency { 2 public static boolean isAlive() { 3 // do something 4 return false; 5 } 6 } 7 public class FlySunDemo { 8 public boolean callStaticMethod() { 9 return ClassDependency.isAlive(); 10 } 11 }
测试用例代码:
1 import org.junit.Assert; 2 import org.junit.Test; 3 import org.junit.runner.RunWith; 4 import org.powermock.api.mockito.PowerMockito; 5 import org.powermock.core.classloader.annotations.PrepareForTest; 6 import org.powermock.modules.junit4.PowerMockRunner; 7 8 @RunWith(PowerMockRunner.class) 9 public class FlySunMockTest { 10 @Test 11 @PrepareForTest(ClassDependency.class) 12 public void testCallFinalMethod() { 13 PowerMockito.mockStatic(ClassDependency.class); 14 PowerMockito.when(ClassDependency.isAlive()).thenReturn(true); 15 FlySunDemo demo = new FlySunDemo(); 16 Assert.assertTrue(demo.callStaticMethod()); 17 } 18 }
说明:当须要mock静态方法的时候,必须加注解@PrepareForTest和@RunWith。注解@PrepareForTest里写的类是静态方法所在的类。
(5) Mock 私有方法
测试目标代码:
1 public class FlySunDemo { 2 public boolean callPrivateMethod() { 3 return isAlive(); 4 } 5 6 private boolean isAlive() { 7 return false; 8 } 9 }
测试用例代码:
1 import org.junit.Assert; 2 import org.junit.Test; 3 import org.junit.runner.RunWith; 4 import org.powermock.api.mockito.PowerMockito; 5 import org.powermock.core.classloader.annotations.PrepareForTest; 6 import org.powermock.modules.junit4.PowerMockRunner; 7 8 @RunWith(PowerMockRunner.class) 9 public class FlySunMockTest { 10 @Test 11 @PrepareForTest(FlySunDemo.class) 12 public void testCallFinalMethod() throws Exception { 13 FlySunDemo demo = PowerMockito.mock(FlySunDemo.class); 14 PowerMockito.when(demo.callPrivateMethod()).thenCallRealMethod(); 15 PowerMockito.when(demo, "isAlive").thenReturn(true); 16 Assert.assertTrue(demo.callPrivateMethod()); 17 } 18 }
注解里写的类是私有方法所在的类。
(6) Mock系统类的静态和final方法
测试目标代码:
1 public class FlySunDemo { 2 public String callSystemStaticMethod(String str) { 3 return System.getProperty(str); 4 } 5 }
测试用例代码:
1 import org.junit.Assert; 2 import org.junit.Test; 3 import org.junit.runner.RunWith; 4 import org.powermock.api.mockito.PowerMockito; 5 import org.powermock.core.classloader.annotations.PrepareForTest; 6 import org.powermock.modules.junit4.PowerMockRunner; 7 8 @RunWith(PowerMockRunner.class) 9 public class FlySunMockTest { 10 @Test 11 @PrepareForTest(FlySunDemo.class) 12 public void testCallSystemStaticMethod(){ 13 FlySunDemo demo = new FlySunDemo(); 14 PowerMockito.mockStatic(System.class); 15 PowerMockito.when(System.getProperty("aaa")).thenReturn("bbb"); 16 Assert.assertEquals("bbb", demo.callSystemStaticMethod("aaa")); 17 } 18 }
说明:和Mock普通对象的静态方法、final方法同样
六 、无所不能的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
7、PowerMock简单实现原理
• 当某个测试方法被注解@PrepareForTest标注之后,在运行测试用例时,会建立一个新的org.powermock.core.classloader.MockClassLoader实例,而后加载该测试用例使用到的类(系统类除外)。
• PowerMock会根据你的mock要求,去修改写在注解@PrepareForTest里的class文件(当前测试类会自动加入注解中),以知足特殊的mock需求。例如:去除final方法的final标识,在静态方法的最前面加入本身的虚拟实现等。
• 若是须要mock的是系统类的final方法和静态方法,PowerMock不会直接修改系统类的class文件,而是修改调用系统类的class文件,以知足mock需求。
原文地址:http://blog.csdn.net/knighttools/article/details/44630975