小编典典

如何验证没有使用 Mockito 调用特定方法?

all

如何验证 没有 在对象的依赖项上调用方法?

例如:

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??**
    }
}

阅读 128

收藏
2022-03-02

共1个答案

小编典典

更有意义:

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

2022-03-02