美文网首页
Mockito中四种生成mock对象的方法

Mockito中四种生成mock对象的方法

作者: 一路花开_8fab | 来源:发表于2019-04-14 20:57 被阅读0次

    该系列文章翻译自https://www.baeldung.com/mockito-series

    1.最简单的mock方法

    public static <T> T mock(Class<T> classToMock)
    

    举例如下:

    @Test
        public void whenUsingSimpleMock_thenCorrect() {
            MyList listMock = mock(MyList.class);
            when(listMock.add(anyString())).thenReturn(false);
            boolean added = listMock.add(randomAlphabetic(6));
    
            verify(listMock).add(anyString());
            assertThat(added, is(false));
        }
    

    2.设置mock对象的名称

    public static <T> T mock(Class<T> classToMock, String name)
    

    举例如下:

        @Rule
        public ExpectedException thrown = ExpectedException.none();
    
        @Test
        public void whenUsingMockWithName_thenCorrect() {
            MyList listMock = mock(MyList.class, "myMock");
            when(listMock.add(anyString())).thenReturn(false);
            listMock.add(randomAlphabetic(6));
            
            thrown.expect(TooLittleActualInvocations.class);
            thrown.expectMessage(containsString("myMock.add"));
    
            verify(listMock, times(2)).add(anyString());
        }
    

    其中,listMock只做了一次add操作,因此,verify(listMock, times(2)).add(anyString())会抛出TooLittleActualInvocations类型的错误,可以看到,在错误详情中显示的mock对象名称,就是我们在创建mock对象时指定的值myMock.

    org.mockito.exceptions.verification.TooLittleActualInvocations:
    myMock.add(<any>);
    Wanted 2 times:
    at com.baeldung.mockito.MockitoMockTest
      .whenUsingMockWithName_thenCorrect(MockitoMockTest.java:...)
    but was 1 time:
    at com.baeldung.mockito.MockitoMockTest
      .whenUsingMockWithName_thenCorrect(MockitoMockTest.java:...)
    

    3.设置mock对象的默认返回

    在创建mock对象时,可以指定默认返回。如下例:创建mock对象时,指定默认返回为false.

        private static class CustomAnswer implements Answer<Boolean> {
            @Override
            public Boolean answer(InvocationOnMock invocation) throws Throwable {
                return false;
            }
        }
    
        @Test
        public void whenUsingMockWithAnswer_thenCorrect() {
            MyList listMock = mock(MyList.class, new CustomAnswer());
            boolean added = listMock.add(randomAlphabetic(6));
    
            verify(listMock).add(anyString());
            assertThat(added, is(false));
        }
    

    4.自定义属性

    MockSettings接口支持自定义的设置,比如下例中,通过defaultAnswer自定义一个默认返回

        private static class CustomAnswer implements Answer<Boolean> {
            @Override
            public Boolean answer(InvocationOnMock invocation) throws Throwable {
                return false;
            }
        }
    
        @Test
        public void whenUsingMockWithSettings_thenCorrect() {
            MockSettings customSettings = withSettings().defaultAnswer(new CustomAnswer());
            MyList listMock = mock(MyList.class, customSettings);
            boolean added = listMock.add(randomAlphabetic(6));
    
            verify(listMock).add(anyString());
            assertThat(added, is(false));
        }
    

    相关文章

      网友评论

          本文标题:Mockito中四种生成mock对象的方法

          本文链接:https://www.haomeiwen.com/subject/njeswqtx.html