一、什麼是mock測試,什麼是mock對象?
先來看看下面這個示例:
從上圖可以看出如果我們要對A進行測試,那麼就要先把整個依賴樹構建出來,也就是BCDE的實例。
一種替代方案就是使用mocks
從圖中可以清晰的看出
mock對象就是在調試期間用來作爲真實對象的替代品。
mock測試就是在測試過程中,對那些不容易構建的對象用一個虛擬對象來代替測試的方法就叫mock測試。
知道什麼是mock測試後,那麼我們就來認識一下mock框架---Mockito
二、什麼是Mockito
除了有一個好記的名字外,Mockito嘗試用不一樣的方法做mocking測試,是簡單輕量級能夠替代EasyMock的框架。使用簡單,測試代碼可讀性高,豐富的文檔包含在javadoc中,直接在IDE中可查看文檔,實例,說明。更多信息:http://code.google.com/p/mockito/
三、Stub和Mock
相同點:Stub和Mock對象都是用來模擬外部依賴,使我們能控制。
不同點:而stub完全是模擬一個外部依賴,用來提供測試時所需要的測試數據。而mock對象用來判斷測試是否能通過,也就是用來驗證測試中依賴對象間的交互能否達到預期。在mocking框架中mock對象可以同時作爲stub和mock對象使用,兩者並沒有嚴格區別。 更多信息:http://martinfowler.com/articles/mocksArentStubs.html
四、mockito入門實例
Maven依賴:(沒用maven管理的可以下載相關jar包導入classpath)
- <dependencies>
- <dependency>
- <groupId>org.mockito</groupId>
- <artifactId>mockito-all</artifactId>
- <version>1.8.5</version>
- <scope>test</scope>
- </dependency>
- </dependencies>
- import static org.mockito.Mockito.*;
- import java.util.List;
- import org.junit.Assert;
- import org.junit.Test;
- /**
- *
- * @author lzjun
- * @version 0.1
- * @date 2012-5-5
- * {@link http://weibo.com/u/1697702241}
- *
- */
- public class SimpleTest {
- @Test
- public void simpleTest(){
- //創建mock對象,參數可以是類,也可以是接口
- List<String> list = mock(List.class);
- //設置方法的預期返回值
- when(list.get(0)).thenReturn("helloworld");
- String result = list.get(0);
- //驗證方法調用(是否調用了get(0))
- verify(list).get(0);
- //junit測試
- Assert.assertEquals("helloworld", result);
- }
- }
好了,五分鐘差不多了,還想繼續瞭解那就可以往下面看
創建mock對象不能對final,Anonymous ,primitive類進行mock。
可對方法設定返回異常
- when(list.get(1)).thenThrow(new RuntimeException("test excpetion"));
stubbing另一種語法(設置預期值的方法),可讀性不如前者
- doReturn("secondhello").when(list).get(1);
沒有返回值的void方法與其設定(支持迭代風格,第一次調用donothing,第二次dothrow拋出runtime異常)
- doNothing().doThrow(new RuntimeException("void exception")).when(list).clear();
- list.clear();
- list.clear();
- verify(list,times(2)).clear();
五、參數匹配器(Argument Matcher)
Matchers類內加你有很多參數匹配器 anyInt、anyString、anyMap.....Mockito類繼承於Matchers,Stubbing時使用內建參數匹配器,下例:
- @Test
- public void argumentMatcherTest(){
- List<String> list = mock(List.class);
- when(list.get(anyInt())).thenReturn("hello","world");
- String result = list.get(0)+list.get(1);
- verify(list,times(2)).get(anyInt());
- Assert.assertEquals("helloworld", result);
- }
需要注意的是:如果使用參數匹配器,那麼所有的參數都要使用參數匹配器,不管是stubbing還是verify的時候都一樣。
- @Test
- public void argumentMatcherTest2(){
- Map<Integer,String> map = mock(Map.class);
- when(map.put(anyInt(),anyString())).thenReturn("hello");//anyString()替換成"hello"就會報錯
- map.put(1, "world");
- verify(map).put(eq(1), eq("world")); //eq("world")替換成"world"也會報錯
- }
六、方法調用的驗證(具體的調用次數、至少一次,一次也沒有)
- @Test
- public void verifyInvocate(){
- List<String> mockedList = mock(List.class);
- //using mock
- mockedList.add("once");
- mockedList.add("twice");
- mockedList.add("twice");
- mockedList.add("three times");
- mockedList.add("three times");
- mockedList.add("three times");
- /**
- * 基本的驗證方法
- * verify方法驗證mock對象是否有沒有調用mockedList.add("once")方法
- * 不關心其是否有返回值,如果沒有調用測試失敗。
- */
- verify(mockedList).add("once");
- verify(mockedList, times(1)).add("once");//默認調用一次,times(1)可以省略
- verify(mockedList, times(2)).add("twice");
- verify(mockedList, times(3)).add("three times");
- //never()等同於time(0),一次也沒有調用
- verify(mockedList, times(0)).add("never happened");
- //atLeastOnece/atLeast()/atMost()
- verify(mockedList, atLeastOnce()).add("three times");
- verify(mockedList, atLeast(2)).add("twice");
- verify(mockedList, atMost(5)).add("three times");
- }