mockito5.4.0单元测试(8) --验证某个方法从未被调用never和verifyNoMoreInteractions
mockito官方文档地址: https://www.javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/Mockito.html#30
【case 1】
//using mocks - only mockOne is interacted
mockOne.add("one");
//ordinary verification
verify(mockOne).add("one"); // 验证调用过add one
//verify that method was never called on a mock
verify(mockOne, never()).add("two"); // 验证从未调用过add two
【case 2】
// mock一个对象
List singleMock = mock(List.class);
// 进行交互
singleMock.add("a");
// 验证这个mock对象从未被交互过,因为上一行已交互过,所以这一行会报错,如果上一行被注释掉,则这一行不会报错。
verifyNoMoreInteractions(singleMock);
end.
![](https://images.cnblogs.com/cnblogs_com/zhuwenjoyce/945554/o_201113020411my-zhifubao-shouqianma.jpeg)