如何验证未对对象的依赖项调用方法?
例如:
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??**
}
}
never
是最好和最具体的方法,但是如果您需要检查整个模拟对象,请考虑verifyZeroInteractions(mockObject)
或verifyNoMoreInteractions(mockObject)
。