import static org.mockito.Mockito.*;
import java.util.List;
import org.junit.Test;
public class TestMockito {
@Test
public void testMockito1(){
// 创建模拟对象
List mockedList = mock(List.class);
//using mock object
mockedList.add("one");
mockedList.clear();
// 验证add方法是否在前面被调用了一次,且参数为“one”。clear方法同样。
verify(mockedList).add("one");
verify(mockedList).clear();
// 下面的验证会失败。因为没有调用过add("two")。
verify(mockedList).add("two");
}
} @Test
public void testMockito2() {
// 我们不仅可以模拟接口还可以模拟具体类。
LinkedList mockedList = mock(LinkedList.class);
// stubbing 当get(0)被调用时,返回"first". 方法get(1)被调用时,抛异常
when(mockedList.get(0)).thenReturn("first");
when(mockedList.get(1)).thenThrow(new RuntimeException());
// 下面会输出"first"
System.out.println(mockedList.get(0));
// 下面会输出"null",因为999没有被设置期望值
System.out.println(mockedList.get(999));
// 下面会抛出异常,因为设置的返回值是异常(想要testMokito2测试通过,将下面这一行get(1)注释即可)
System.out.println(mockedList.get(1));
//重复stub两次,则以第二次为准。如下将返回"second":
when(mockedList.get(0)).thenReturn("first");
when(mockedList.get(0)).thenReturn("second");
// 输出"second"
System.out.println(mockedList.get(0));
//如果是下面这种形式,则表示第一次调用时返回“first”,第二次调用时返回“second”。可以写n多个。
when(mockedList.get(0)).thenReturn("first").thenReturn("second");
// 输出"first"
System.out.println(mockedList.get(0));
// 输出"second"
System.out.println(mockedList.get(0));
// 输出"second"
System.out.println(mockedList.get(0));
} @Test
public void testMockito3() {
LinkedList mockedList = mock(LinkedList.class);
// 使用內置的參數匹配函數anyInt()
when(mockedList.get(anyInt())).thenReturn("element");
// 也可以使用自定義的匹配類,比如下面的isValid就是一個自定義的匹配器
when(mockedList.contains(argThat(isValid()))).thenReturn(false);
// 下面會輸出"element"
System.out.println(mockedList.get(999));
// 也可以通過參數匹配方法進行驗證
verify(mockedList).get(anyInt());
// 下面的方法會報錯,因為第一個參數沒有使用參數匹配,而第二個參數使用了參數匹配
verify(mockedList).set(1, anyString());
}
private Matcher<String> isValid(){
return Matchers.any();
} @Test
public void testMockito4() {
LinkedList mockedList = mock(LinkedList.class);
// using mock
mockedList.add("once");
mockedList.add("twice");
mockedList.add("twice");
mockedList.add("three times");
mockedList.add("three times");
mockedList.add("three times");
// 下面兩種寫法都是針對調用1次,因為1是默認的,我們一般使用第一種寫法就可以
verify(mockedList).add("once");
verify(mockedList, times(1)).add("once");
// 指定了具體調用的次數
verify(mockedList, times(2)).add("twice");
verify(mockedList, times(3)).add("three times");
// 使用了never(),即times(0)
verify(mockedList, never()).add("never happened");
// 使用了最多多少次,最少多少次
verify(mockedList, atLeastOnce()).add("three times");
verify(mockedList, atLeast(2)).add("three times");
verify(mockedList, atMost(5)).add("three times");
} @Test
public void testMockito6(){
List firstMock = mock(List.class);
List secondMock = mock(List.class);
//using mocks
firstMock.add("was called first");
secondMock.add("was called second");
// 创建InOrder对象时只需要传入你所需要验证顺序的Mock对象即可
InOrder inOrder = inOrder(firstMock, secondMock);
// 下面这两个是正确的,调用顺序正确
inOrder.verify(firstMock).add("was called first");
inOrder.verify(secondMock).add("was called second");
// 下面这两个会失败,因为调用的顺序出错了
inOrder.verify(secondMock).add("was called second");
inOrder.verify(firstMock).add("was called first");
}原文地址:http://blog.csdn.net/dandan8866/article/details/42294679