本文介绍了如何使用 Mockito 验证未调用特定方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何验证一个方法未在对象的依赖项上调用?
How to verify that a method is not called on an object's dependency?
例如:
public interface Dependency {
void someMethod();
}
public class Foo {
public bar(final Dependency d) {
...
}
}
使用 Foo 测试:
public class FooTest {
@Test
public void dependencyIsNotCalled() {
final Foo foo = new Foo(...);
final Dependency dependency = mock(Dependency.class);
foo.bar(dependency);
**// verify here that someMethod was not called??**
}
}
推荐答案
更有意义:
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
// ...
verify(dependency, never()).someMethod();
此功能的文档在§4验证确切的调用次数/至少 x/从不",never
javadoc 是 这里.
The documentation of this feature is there §4 "Verifying exact number of invocations / at least x / never", and the never
javadoc is here.
这篇关于如何使用 Mockito 验证未调用特定方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!