mockito

一、函数

方法 作用
mock() 根据数据模板生成模拟数据
verify() 选择性、显式、可读性高的验证(verify验证调用次数,默认为1)
when(mockedList.get(0)).thenReturn(“first”); 当运行mockedList.get(0)方法时,返回first
verifyNoMoreInteractions() verifyNoMoreInteractions()方法可以传入多个,mock对象作为参数,用来验证传入的这些mock对象是否存在没有验证过的调用方法
doThrow(new RuntimeException()).when(mockedList).clear(); 执行mockedList.clear()方法时,抛出RuntimeException()异常
verifyNoMoreInteractions() verifyNoMoreInteractions()方法可以传入多个mock对象作为参数,用来验证传入的这些mock对象是否存在没有验证过的调用方法。

二、示例

1.验证方法的调用次数

mockito

2.when().thenReturn()方法

mockitomockito

3.verify()方法

mockito

4.doThrow()

mockito

5.verifyNoMoreInteractions()

mockito
mockito

6.when().thenReturn();

(1)when().thenThrow().thenReturn()

mockitomockitomockito

(2)when().thenReturn(“one”, “two”, “three”);

mockitomockito

(3)两个when().thenReturn()会覆盖

mockitomockito
github源码:https://github.com/qq2393436696/mockito-demo