mock框架官方文档地址:https://javadoc.io/static/org.mockito/mockito-core/3.2.0/org/mockito/Mockito.html#verification

1. Mock一个对象

 //Let's import Mockito statically so that the code looks clearerimport static org.mockito.Mockito.*;//mock creationList mockedList = mock(List.class);//using mock objectmockedList.add("one");mockedList.clear();//verificationverify(mockedList).add("one");verify(mockedList).clear();

2.添加预期返回结果

 //You can mock concrete classes, not just interfacesLinkedList mockedList = mock(LinkedList.class);//stubbing 设置预期值,获取第一个值返回"first",获取第二个值时抛出异常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 exceptionSystem.out.println(mockedList.get(1));//following prints "null" because get(999) was not stubbedSystem.out.println(mockedList.get(999));

3.校验方法入参

 //stubbing using built-in anyInt() argument matcher  //设置预期返回结果 输入任何int类型的值都返回 "element"when(mockedList.get(anyInt())).thenReturn("element");//stubbing using custom matcher (let's say isValid() returns your own matcher implementation):
// 设置预期结果  调用contains()方法时返回 truewhen(mockedList.contains(argThat(isValid()))).thenReturn(true);//following prints "element"System.out.println(mockedList.get(999));//you can also verify using an argument matcherverify(mockedList).get(anyInt());

如果对一个方法使用了参数校验,那么这个方法的所有参数都要进行校验

    //above is correct - eq() is also an argument matcher  这个方法的所有参数都进行了校验,正确verify(mock).someMethod(anyInt(), anyString(), eq("third argument"));//above is incorrect - exception will be thrown because third argument is given without an argument matcher. 这个方法的第三个入参没有进行校验,不正确verify(mock).someMethod(anyInt(), anyString(), "third argument");

4.校验调用次数,至少xx次,没有调用

 //using mockmockedList.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 verificationverify(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");

5.抛出异常

doThrow(new RuntimeException()).when(mockedList).clear();//following throws RuntimeException:mockedList.clear();

6.校验调用顺序

// A. Single mock whose methods must be invoked in a particular orderList singleMock = mock(List.class);//using a single mocksingleMock.add("was added first");singleMock.add("was added second");//create an inOrder verifier for a single mockInOrder inOrder = inOrder(singleMock);//following will make sure that add is first called with "was added first", then with "was added second"inOrder.verify(singleMock).add("was added first");inOrder.verify(singleMock).add("was added second");// B. Multiple mocks that must be used in a particular orderList firstMock = mock(List.class);List secondMock = mock(List.class);//using mocksfirstMock.add("was called first");secondMock.add("was called second");//create inOrder object passing any mocks that need to be verified in orderInOrder inOrder = inOrder(firstMock, secondMock);//following will make sure that firstMock was called before secondMockinOrder.verify(firstMock).add("was called first");inOrder.verify(secondMock).add("was called second");// Oh, and A + B can be mixed together at will

7. 校验调用关系

 //using mocks - only mockOne is interactedmockOne.add("one");//ordinary verificationverify(mockOne).add("one");//verify that method was never called on a mockverify(mockOne, never()).add("two");//verify that other mocks were not interactedverifyZeroInteractions(mockTwo, mockThree);

8.

java单元测试之Mock测试编写相关推荐

  1. java单元测试之mock篇

    java单元测试之mock篇 一.什么是mock? 二.为什么要进行mock? 三.IDEA中使用Mock 3.1.引入mock所需依赖 3.1.IDEA单元测试必备快捷键 3.2.Mock测试相关注 ...

  2. Java单元测试之Mock框架

    一.引言 二.为什么要用Mock 三.Mock使用场景 四.Mock定义 五.Mock框架 五.Mockito 5.1 Mockito基本使用 5.2 MockMVC测试 5.2.1 初始化MockM ...

  3. Java单元测试之模拟利器-使用PowerMock进行Mock测试

    首页 国产Linux Linux命令 openSUSE ArchLinux Slackware FreeBSD Ubuntu CentOS Fedora Debian PHP教程 在线教程 登录 注册 ...

  4. Java单元测试之JUnit4详解

    2019独角兽企业重金招聘Python工程师标准>>> Java单元测试之JUnit4详解 与JUnit3不同,JUnit4通过注解的方式来识别测试方法.目前支持的主要注解有: @B ...

  5. Java基础学习总结(24)——Java单元测试之JUnit4详解

    Java单元测试之JUnit4详解 与JUnit3不同,JUnit4通过注解的方式来识别测试方法.目前支持的主要注解有: @BeforeClass 全局只会执行一次,而且是第一个运行 @Before  ...

  6. Android 单元测试之UI测试

    Android 单元测试之UI测试 UI测试 Espresso 官网地址 Espresso是Google官方的一个针对Android UI测试的库,可以自动化的进行UI测试. Espresso可以验证 ...

  7. 单元测试之mock使用

    目录 一.简介 二.使用mock做单元测试的优点 1.效率高 2.TDD(测试驱动开发) 3.并行开发 4.解决环境依赖问题 三.mock实现原理 四.mock使用 1.导入Maven依赖包 2.编写 ...

  8. java的单元测试-mock测试

    文章目录 简介 Assert mockMVC 优化 mockBean(模拟bean和测试接口) 简介 对于普通的方法,通常采用断言测试. 对于接口,需要使用mockMvc 对于未开发的功能,需要moc ...

  9. SpringBoot单元测试之mock静态方法

    The article summary 1 为什么要对静态方法mock 2 如何使用`powermock`对静态方法`mock` 2.1 添加依赖 2.2 编写测试类 2.3 需要注意的问题 1 为什 ...

最新文章

  1. 自定义定时器的一点总结
  2. c语言答案填空选择,C语言试题配答案
  3. Coolite Cool Study 3 MVC + Coolite 的例子
  4. 调用函数,整数逆序输出
  5. 一 ASP.NET Html 表单
  6. SharePoint 2010 初体验(二)搭建一个简单的三态工作流
  7. display:flex弹性布局
  8. 使用DynamoDB映射器将DynamoDB项目映射到对象
  9. STM32位带操作实现过程解析
  10. php数值操作,php数值计算num类简单操作示例
  11. urllib.error.HTTPError: HTTP Error 403: Forbidden
  12. c语言-基本计算 pm2.5,C语言程序设计题(A卷).doc
  13. php判断服务器操作系统的类型
  14. linux c开发项目,linux c 服务器开发项目
  15. DoTween详细使用教程
  16. 大象---thinking in UML
  17. Android 鼠标样式修改
  18. 18本生物竞赛辅导书
  19. 查看、修改图片gps地理位置信息
  20. 计算机在职博士要考吗,在职博士容易考吗?

热门文章

  1. xls表格删除了可以恢复吗
  2. python pip安装selenium_python+selenium安装
  3. 2022年十大接口测试工具合集《建议.收藏》
  4. Android把商品添加到购物车的动画效果(贝塞尔曲线)
  5. jsp连接mysql详解
  6. python学习 | 视频的封面提取
  7. Windchill DB與Aphelion
  8. 7 For All Mankind推出N°21 x 7 For All Mankind 胶囊系列 庆祝品牌创立21周年
  9. Go 接口,接口继承
  10. K到曝欢唱版评测:给果粉K歌的SNS