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" ); |