如何验证 没有 在对象的依赖项上调用方法?
例如:
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“Verifying exact number of invocations / at least x / never”,neverjavadoc是here。
never