在Java中使用Mockito如何验证方法只调用一次精确参数忽略对其他方法的调用?
示例代码:
public class MockitoTest {
interface Foo {
void add(String str);
void clear();
}
@Test
public void testAddWasCalledOnceWith1IgnoringAllOtherInvocations() throws Exception {
// given
Foo foo = Mockito.mock(Foo.class);
// when
foo.add("1"); // call to verify
foo.add("2"); // !!! don't allow any other calls to add()
foo.clear(); // calls to other methods should be ignored
// then
Mockito.verify(foo, Mockito.times(1)).add("1");
// TODO: don't allow all other invocations with add()
// but ignore all other calls (i.e. the call to clear())
}
} …Run Code Online (Sandbox Code Playgroud) 在Mockito,有没有办法验证我创建的任何模拟都没有更多的交互?
例如:
public void test()
{
...
TestObject obj = mock(TestObject);
myClass.test();
verifyNoMoreInteractionsWithMocks(); <-------
}
Run Code Online (Sandbox Code Playgroud)
有这样的方法吗?