是时候搞搞单元测了

作者: lucode | 来源:发表于2018-12-16 09:52 被阅读0次

    官方文档
    第一行代码,静态导入

    import static org.mockito.Mockito.*;
    

    测试桩 (Stub)

    常用的当在调用外部接口的时候可以使用测试桩返回固定的一些值

     //You can mock concrete classes, not only interfaces
     // 你可以mock具体的类型,不仅只是接口
     LinkedList mockedList = mock(LinkedList.class);
    
     //stubbing 测试桩
     when(mockedList.get(0)).thenReturn("first");
     when(mockedList.get(1)).thenThrow(new RuntimeException());
    
     //following prints "first" 输出“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
     // 因为get(999) 没有打桩,因此输出null
     System.out.println(mockedList.get(999));
    

    mock 异常抛出

    无返回值

    doThrow(new RuntimeException()).when(mockedList).clear();
    

    有返回值的

    when(mockedList.get(anyInt())).thenThrow(new RuntimeException());
    

    参数匹配器 (matchers)

    当参数可能不确定的时候 anyInt() 来匹配

    // 使用内置的anyInt()参数匹配器
     when(mockedList.get(anyInt())).thenReturn("element");
    

    为连续的调用做测试桩 (stub)

    List mockedList = mock(List.class);
    when(mockedList.get(anyInt()))
                .thenThrow(new RuntimeException())
                .thenReturn("foo");
    // First call: throws runtime exception:
    // 第一次调用 : 抛出运行时异常
    try {
         mockedList.get(2);
    } catch (Exception e) {
         System.out.println("有异常");
    }
    // Second call: prints "foo"
    // 第二次调用 : 输出"foo"
    System.out.println(mockedList.get(3));
    // Any consecutive call: prints "foo" as well (last stubbing wins).
    // 后续调用 : 也是输出"foo"
    System.out.println(mockedList.get(4));
    

    简化写法

    // 第一次调用时返回"one",第二次返回"two",第三次返回"three"
     when(mock.someMethod("some arg"))
       .thenReturn("one", "two", "three");
    
    

    为回调写测试桩(有返回值)

     when(mock.someMethod(anyString())).thenAnswer(new Answer() {
         Object answer(InvocationOnMock invocation) {
             Object[] args = invocation.getArguments();
             Object mock = invocation.getMock();
             return "called with arguments: " + args;
         }
     });
     //Following prints "called with arguments: foo"
     // 输出 : "called with arguments: foo"
     System.out.println(mock.someMethod("foo"));
    
            List mockedList = mock(List.class);
            mockedList.add("haha");
            when(mockedList.get(anyInt())).thenAnswer(
                (invocation) -> {
                    // 获取所有参数
                    Object[] args = invocation.getArguments();
                    // 获取第一个参数
                    Object firstArgs = invocation.getArgument(0);
                    return "called with arguments: " + firstArgs;
                });
            // 最后答应出 回调函数中的东西
            System.out.println(mockedList.get(2));
    

    回调测试桩

    这样写可能更好,注意这时候when监控的是 mockedList

            doAnswer(x->{
                System.out.println("clear");
                return null;
            }).when(mockedList).clear();
            mockedList.clear();
    

    verify 验证你的程序

     verify(mockedList, atLeastOnce()).add("three times");
    

    比如 至少被执行两次,如果只 add 一次就会报错

    List mockedList = mock(List.class);
            mockedList.add("one");
            mockedList.add("one");
            verify(mockedList,atLeast(2)).add("one");
    

    相关文章

      网友评论

        本文标题:是时候搞搞单元测了

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