是时候搞搞单元测了

作者: 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");

相关文章

  • 是时候搞搞单元测了

    官方文档第一行代码,静态导入 测试桩 (Stub) 常用的当在调用外部接口的时候可以使用测试桩返回固定的一些值 m...

  • 单元测

    翔同学今天语文和数学都做了单元测。语文优,错了三题,他回来的时候跟我说,今天老师还表扬了他进步了,我也及时做...

  • XCTest入门

    关于XCTest XCTest是苹果官方提供的单元测试框架,由此苹果提供了很详细的文档XCTest。我们写的单元测...

  • 初二语文备组第07次集体备课纪要(2021.10.21)

    主要议题: 1、上周单元测总结 2、第二单元教学问题交流 3、期中备考安排 4、校庆征文活动安排 一、单元测总结 ...

  • 前端单元测试

    前端单元测试是什么? 单元测试(unit testing),是指对软件中的最小可测试单元进行检查和验证。对于单元测...

  • JDBC

    最近学习了Java中的JDBC,做个总结~~ 1.Jdbc基础准备 junit(单元测试的工具) Junit单元测...

  • SpringBoot 单元测试与 Mockito 使用

    SpringBoot 单元测试与 Mockito 使用 单元测试应遵循 → AIR 原则 SpringBoot 测...

  • pytest知识点

    一、单元测试框架1.什么是单元测试框架单元测试框架是在自动化测试或者白盒测试中对软件的最小单元(函数、方法)进行测...

  • 长度测量单元小测:测了啥?测得咋样?

    我认为测量板块的探究学习,对于孩子们认识世界,从具象到抽象,进一步发展数学思维,有着至关重要的价值。 在一二年级学...

  • 公众号做活动,怎么才能让粉丝配合你?

    都说 这个年代做运营 得写得了文案做得了营销 更要搞搞搞搞搞搞搞搞搞得了活动 (还要是刷屏级的那种)公众号加粉 而...

网友评论

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

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