PowerMockito使用詳解

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

相關文章
相關標籤/搜索