Mockito
1、添加依赖
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<scope>test</scope>
</dependency>
2、开启Mock
2.1 Mockito.mock
直接使用Mockito提供的mock方法即可以模拟出一个服务的实例。
public class MockitoAnnotationTest {
@Test
public void whenNotUseMockAnnotation_thenCorrect() {
//mock creation
List mockedList = Mockito.mock(List.class);
}
}
2.2 MockitoAnnotations.initMocks(this)
这里给出了使用@Mock注解来Mock对象时的第一种实现,即使用MockitoAnnotations.initMocks(testClass)。
public class MockitoAnnotationTest {
@Mock
List<String> mockedList;
@Before
public void before() {
//MockitoAnnotations.initMocks(this); //initMocks方法已过时
MockitoAnnotations.openMocks(this);
}
@Test
public void whenUseMockAnnotation_thenMockIsInjected() {
mockedList.add("one");
}
}
2.3 @RunWith(MockitoJUnitRunner.class)
在测试用例上带上了这个注解后,就可以自由的使用@Mock来Mock对象啦。
@RunWith(MockitoJUnitRunner.class)
public class MockitoAnnotationTest {
@Mock
List<String> mockedList;
@Test
public void whenUseMockAnnotation_thenMockIsInjected() {
mockedList.add("one");
}
}
2.4 MockitoRule
这里需要注意的是如果使用MockitoRule的话,该对象的访问级别必须为public。
public class MockitoAnnotationTest {
@Rule
public MockitoRule initRule = MockitoJUnit.rule();
@Mock
List<String> mockedList;
@Test
public void whenUseMockAnnotation_thenMockIsInjected() {
mockedList.add("one");
}
}
在上面四种方法中,最推荐的就是第二种方法,如果无法使用@RunWith(MockitoJUnitRunner.class)
时,再考虑别的兼容的方法。
3、verify
一旦mock对象被创建了,mock对象会记住所有的交互。然后你就可能选择性的验证你感兴趣的交互。
// 静态导入会使代码更简洁
import static org.mockito.Mockito.*;
// mock creation 创建mock对象
List mockedList = mock(List.class);
//using mock object 使用mock对象
mockedList.add("one");
mockedList.clear();
verify(mockedList).add("one");
verify(mockedList).clear();
4、测试桩
LinkedList mockedList = mock(LinkedList.class);
//stubbing
when(mockedList.get(0)).thenReturn("first");
when(mockedList.get(1)).thenThrow(new RuntimeException());
//following prints "first"
System.out.println(mockedList.get(0));
//following throws runtime exception
System.out.println(mockedList.get(1));
//following prints "null" because get(999) was not stubbed
System.out.println(mockedList.get(999));
verify(mockedList).get(0);
doThrow(new RuntimeException()).when(mockedList).clear();
//following throws RuntimeException:
mockedList.clear();
5、参数匹配器
Mockito以自然的java风格来验证参数值: 使用equals()函数。有时,当需要额外的灵活性时你可能需要使用参数匹配器,也就是argument matchers :
//stubbing using built-in anyInt() argument matcher
when(mockedList.get(anyInt())).thenReturn("element");
//stubbing using custom matcher (let's say isValid() returns your own matcher implementation):
when(mockedList.contains(argThat(isValid()))).thenReturn("element");
//following prints "element"
System.out.println(mockedList.get(999));
//you can also verify using an argument matcher
verify(mockedList).get(anyInt());
6、验证调用次数
//using mock
mockedList.add("once");
mockedList.add("twice");
mockedList.add("twice");
mockedList.add("three times");
mockedList.add("three times");
mockedList.add("three times");
//following two verifications work exactly the same - times(1) is used by default
verify(mockedList).add("once");
verify(mockedList, times(1)).add("once");
//exact number of invocations verification
verify(mockedList, times(2)).add("twice");
verify(mockedList, times(3)).add("three times");
//verification using never(). never() is an alias to times(0)
verify(mockedList, never()).add("never happened");
//verification using atLeast()/atMost()
verify(mockedList, atMostOnce()).add("once");
verify(mockedList, atLeastOnce()).add("three times");
verify(mockedList, atLeast(2)).add("three times");
verify(mockedList, atMost(5)).add("three times");
//verify that method was never called on a mock
verify(mockOne, never()).add("never");
参考资料: