Mockito

1、添加依赖

  1. <dependency>
  2. <groupId>org.mockito</groupId>
  3. <artifactId>mockito-all</artifactId>
  4. <scope>test</scope>
  5. </dependency>

2、开启Mock

2.1 Mockito.mock

直接使用Mockito提供的mock方法即可以模拟出一个服务的实例。

  1. public class MockitoAnnotationTest {
  2. @Test
  3. public void whenNotUseMockAnnotation_thenCorrect() {
  4. //mock creation
  5. List mockedList = Mockito.mock(List.class);
  6. }
  7. }

2.2 MockitoAnnotations.initMocks(this)

这里给出了使用@Mock注解来Mock对象时的第一种实现,即使用MockitoAnnotations.initMocks(testClass)。

  1. public class MockitoAnnotationTest {
  2. @Mock
  3. List<String> mockedList;
  4. @Before
  5. public void before() {
  6. //MockitoAnnotations.initMocks(this); //initMocks方法已过时
  7. MockitoAnnotations.openMocks(this);
  8. }
  9. @Test
  10. public void whenUseMockAnnotation_thenMockIsInjected() {
  11. mockedList.add("one");
  12. }
  13. }

2.3 @RunWith(MockitoJUnitRunner.class)

在测试用例上带上了这个注解后,就可以自由的使用@Mock来Mock对象啦。

  1. @RunWith(MockitoJUnitRunner.class)
  2. public class MockitoAnnotationTest {
  3. @Mock
  4. List<String> mockedList;
  5. @Test
  6. public void whenUseMockAnnotation_thenMockIsInjected() {
  7. mockedList.add("one");
  8. }
  9. }

2.4 MockitoRule

这里需要注意的是如果使用MockitoRule的话,该对象的访问级别必须为public。

  1. public class MockitoAnnotationTest {
  2. @Rule
  3. public MockitoRule initRule = MockitoJUnit.rule();
  4. @Mock
  5. List<String> mockedList;
  6. @Test
  7. public void whenUseMockAnnotation_thenMockIsInjected() {
  8. mockedList.add("one");
  9. }
  10. }

在上面四种方法中,最推荐的就是第二种方法,如果无法使用@RunWith(MockitoJUnitRunner.class)时,再考虑别的兼容的方法。

3、verify

一旦mock对象被创建了,mock对象会记住所有的交互。然后你就可能选择性的验证你感兴趣的交互。

  1. // 静态导入会使代码更简洁
  2. import static org.mockito.Mockito.*;
  3. // mock creation 创建mock对象
  4. List mockedList = mock(List.class);
  5. //using mock object 使用mock对象
  6. mockedList.add("one");
  7. mockedList.clear();
  8. verify(mockedList).add("one");
  9. verify(mockedList).clear();

4、测试桩

  1. LinkedList mockedList = mock(LinkedList.class);
  2. //stubbing
  3. when(mockedList.get(0)).thenReturn("first");
  4. when(mockedList.get(1)).thenThrow(new RuntimeException());
  5. //following prints "first"
  6. System.out.println(mockedList.get(0));
  7. //following throws runtime exception
  8. System.out.println(mockedList.get(1));
  9. //following prints "null" because get(999) was not stubbed
  10. System.out.println(mockedList.get(999));
  11. verify(mockedList).get(0);
  12. doThrow(new RuntimeException()).when(mockedList).clear();
  13. //following throws RuntimeException:
  14. mockedList.clear();

5、参数匹配器

Mockito以自然的java风格来验证参数值: 使用equals()函数。有时,当需要额外的灵活性时你可能需要使用参数匹配器,也就是argument matchers :

  1. //stubbing using built-in anyInt() argument matcher
  2. when(mockedList.get(anyInt())).thenReturn("element");
  3. //stubbing using custom matcher (let's say isValid() returns your own matcher implementation):
  4. when(mockedList.contains(argThat(isValid()))).thenReturn("element");
  5. //following prints "element"
  6. System.out.println(mockedList.get(999));
  7. //you can also verify using an argument matcher
  8. verify(mockedList).get(anyInt());

6、验证调用次数

  1. //using mock
  2. mockedList.add("once");
  3. mockedList.add("twice");
  4. mockedList.add("twice");
  5. mockedList.add("three times");
  6. mockedList.add("three times");
  7. mockedList.add("three times");
  8. //following two verifications work exactly the same - times(1) is used by default
  9. verify(mockedList).add("once");
  10. verify(mockedList, times(1)).add("once");
  11. //exact number of invocations verification
  12. verify(mockedList, times(2)).add("twice");
  13. verify(mockedList, times(3)).add("three times");
  14. //verification using never(). never() is an alias to times(0)
  15. verify(mockedList, never()).add("never happened");
  16. //verification using atLeast()/atMost()
  17. verify(mockedList, atMostOnce()).add("once");
  18. verify(mockedList, atLeastOnce()).add("three times");
  19. verify(mockedList, atLeast(2)).add("three times");
  20. verify(mockedList, atMost(5)).add("three times");
  21. //verify that method was never called on a mock
  22. verify(mockOne, never()).add("never");

参考资料: