1、为何要使用Mock工具服务器
在作单元测试的时候,咱们会发现咱们要测试的方法会引用不少外部依赖的对象,好比:(发送邮件,网络通信,远程服务, 文件系统等等)。 而咱们无法控制这些外部依赖的对象,为了解决这个问题,咱们就须要用到Mock工具来模拟这些外部依赖的对象,来完成单元测试。网络
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有两个重要的注解:google
–@RunWith(PowerMockRunner.class)spa
–@PrepareForTest( { YourClassWithEgStaticMethod.class })code
若是你的测试用例里没有使用注解@PrepareForTest,那么能够不用加注解@RunWith(PowerMockRunner.class),反之亦然。当你须要使用PowerMock强大功能(Mock静态、final、私有方法等)的时候,就须要加注解@PrepareForTest。
5、PowerMock基本用法
(1) 普通Mock: Mock参数传递的对象
测试目标代码:
1 |
public boolean callArgumentInstance(File file) { |
2 |
3 |
return file.exists(); |
4 |
5 |
} |
测试用例代码:
01 |
@Test |
02 |
public void testCallArgumentInstance() { |
03 |
|
04 |
File file = PowerMockito.mock(File. class ); |
05 |
06 |
ClassUnderTest underTest = new ClassUnderTest(); |
07 |
|
08 |
PowerMockito.when(file.exists()).thenReturn( true ); |
09 |
|
10 |
Assert.assertTrue(underTest.callArgumentInstance(file)); |
11 |
} |
说明:普通Mock不须要加@RunWith和@PrepareForTest注解。
(2) Mock方法内部new出来的对象
测试目标代码:
01 |
public class ClassUnderTest { |
02 |
03 |
public boolean callInternalInstance(String path) { |
04 |
05 |
File file = new File(path); |
06 |
07 |
return file.exists(); |
08 |
09 |
} |
10 |
} |
测试用例代码:
01 |
@RunWith (PowerMockRunner. class ) |
02 |
public class TestClassUnderTest { |
03 |
04 |
@Test |
05 |
@PrepareForTest (ClassUnderTest. class ) |
06 |
public void testCallInternalInstance() throws Exception { |
07 |
08 |
File file = PowerMockito.mock(File. class ); |
09 |
10 |
ClassUnderTest underTest = new ClassUnderTest(); |
11 |
12 |
PowerMockito.whenNew(File. class ).withArguments( "bbb" ).thenReturn(file); |
13 |
|
14 |
PowerMockito.when(file.exists()).thenReturn( true ); |
15 |
16 |
Assert.assertTrue(underTest.callInternalInstance( "bbb" )); |
17 |
} |
18 |
} |
说明:当使用PowerMockito.whenNew方法时,必须加注解@PrepareForTest和@RunWith。注解@PrepareForTest里写的类是须要mock的new对象代码所在的类。
(3) Mock普通对象的final方法
测试目标代码:
1 |
public class ClassUnderTest { |
2 |
3 |
public boolean callFinalMethod(ClassDependency refer) { |
4 |
5 |
return refer.isAlive(); |
6 |
7 |
} |
8 |
} |
01 |
public class ClassDependency { |
02 |
|
03 |
public final boolean isAlive() { |
04 |
05 |
// do something |
06 |
07 |
return false ; |
08 |
09 |
} |
10 |
} |
测试用例代码:
01 |
@RunWith (PowerMockRunner. class ) |
02 |
public class TestClassUnderTest { |
03 |
04 |
@Test |
05 |
@PrepareForTest (ClassDependency. class ) |
06 |
public void testCallFinalMethod() { |
07 |
08 |
ClassDependency depencency = PowerMockito.mock(ClassDependency. class ); |
09 |
|
10 |
ClassUnderTest underTest = new ClassUnderTest(); |
11 |
|
12 |
PowerMockito.when(depencency.isAlive()).thenReturn( true ); |
13 |
|
14 |
Assert.assertTrue(underTest.callFinalMethod(depencency)); |
15 |
|
16 |
} |
17 |
} |
说明: 当须要mock final方法的时候,必须加注解@PrepareForTest和@RunWith。注解@PrepareForTest里写的类是final方法所在的类。
(4) Mock普通类的静态方法
测试目标代码:
1 |
public class ClassUnderTest { |
2 |
3 |
public boolean callStaticMethod() { |
4 |
|
5 |
return ClassDependency.isExist(); |
6 |
7 |
} |
8 |
} |
01 |
public class ClassDependency { |
02 |
|
03 |
public static boolean isExist() { |
04 |
05 |
// do something |
06 |
07 |
return false ; |
08 |
09 |
} |
10 |
} |
测试用例代码:
01 |
@RunWith (PowerMockRunner. class ) |
02 |
public class TestClassUnderTest { |
03 |
04 |
@Test |
05 |
@PrepareForTest (ClassDependency. class ) |
06 |
public void testCallStaticMethod() { |
07 |
|
08 |
ClassUnderTest underTest = new ClassUnderTest(); |
09 |
|
10 |
PowerMockito.mockStatic(ClassDependency. class ); |
11 |
12 |
PowerMockito.when(ClassDependency.isExist()).thenReturn( true ); |
13 |
|
14 |
Assert.assertTrue(underTest.callStaticMethod()); |
15 |
|
16 |
} |
17 |
} |
说明:当须要mock静态方法的时候,必须加注解@PrepareForTest和@RunWith。注解@PrepareForTest里写的类是静态方法所在的类。
(5) Mock 私有方法
测试目标代码:
01 |
public class ClassUnderTest { |
02 |
03 |
public boolean callPrivateMethod() { |
04 |
05 |
return isExist(); |
06 |
07 |
} |
08 |
09 |
private boolean isExist() { |
10 |
|
11 |
return false ; |
12 |
13 |
} |
14 |
} |
测试用例代码:
01 |
@RunWith (PowerMockRunner. class ) |
02 |
public class TestClassUnderTest { |
03 |
04 |
@Test |
05 |
@PrepareForTest (ClassUnderTest. class ) |
06 |
public void testCallPrivateMethod() throws Exception { |
07 |
08 |
ClassUnderTest underTest = PowerMockito.mock(ClassUnderTest. class ); |
09 |
10 |
PowerMockito.when(underTest.callPrivateMethod()).thenCallRealMethod(); |
11 |
12 |
PowerMockito.when(underTest, "isExist" ).thenReturn( true ); |
13 |
|
14 |
Assert.assertTrue(underTest.callPrivateMethod()); |
15 |
|
16 |
} |
17 |
} |
说明:和Mock普通方法同样,只是须要加注解@PrepareForTest(ClassUnderTest.class),注解里写的类是私有方法所在的类。
(6) Mock系统类的静态和final方法
测试目标代码:
01 |
public class ClassUnderTest { |
02 |
03 |
public boolean callSystemFinalMethod(String str) { |
04 |
05 |
return str.isEmpty(); |
06 |
07 |
} |
08 |
09 |
public String callSystemStaticMethod(String str) { |
10 |
|
11 |
return System.getProperty(str); |
12 |
13 |
} |
14 |
} |
测试用例代码:
01 |
@RunWith (PowerMockRunner. class ) |
02 |
public class TestClassUnderTest { |
03 |
04 |
@Test |
05 |
@PrepareForTest (ClassUnderTest. class ) |
06 |
public void testCallSystemStaticMethod() { |
07 |
08 |
ClassUnderTest underTest = new ClassUnderTest(); |
09 |
10 |
PowerMockito.mockStatic(System. class ); |
11 |
12 |
PowerMockito.when(System.getProperty( "aaa" )).thenReturn( "bbb" ); |