我想验证是否与may db模拟对象存在x次交互。 这样做是否与'verifyZeroInteractions()'方法相似?
答案 0 :(得分:4)
Mockito.verify(MOCKED_OBJ, Mockito.times(number)).YOUR_METHOD_CALL();
Mockito.verify(MOCKED_OBJ, Mockito.atLeast(number)).YOUR_METHOD_CALL();
更多信息为here
答案 1 :(得分:1)
根据模拟文档,您可以使用verify(mockedList, never()).add("never happened");
。
有很多有用的组合来检查您的预期行为。来自Mockito doc的其他示例:
//using mock
mockedList.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 verification
verify(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, atLeastOnce()).add("three times");
verify(mockedList, atLeast(2)).add("three times");
verify(mockedList, atMost(5)).add("three times");
或者您可以使用verifyNoMoreInteractions来确保没有其他与您的模拟互动。
//using mocks
mockedList.add("one");
mockedList.add("two");
verify(mockedList).add("one");
//following verification will fail
verifyNoMoreInteractions(mockedList);
此外,您可以使用inOrder来以完全相同的顺序验证多个模拟的交互,并添加verifyNoMoreInteractions()以确保没有其他与模拟定义的交互。
InOrder inOrder = inOrder(firstMock, secondMock);
inOrder.verify(firstMock).add("was called first");
inOrder.verify(secondMock).add("was called second");
inOrder.verifyNoMoreInteractions();
检查Mockito文档,以了解模拟的全部功能: https://static.javadoc.io/org.mockito/mockito-core/2.27.0/org/mockito/Mockito.html
另请参阅 https://static.javadoc.io/org.mockito/mockito-core/2.27.0/org/mockito/Mockito.html#4 https://static.javadoc.io/org.mockito/mockito-core/2.27.0/org/mockito/Mockito.html#8
答案 2 :(得分:0)
您可以使用Mockito.verify()
方法,将Mockito.times(...)
或Mockito.atLeast(..)
用作参数。
答案 3 :(得分:0)
或更优雅
verify(MOCK, never()).method()