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

问题:

如何验证一个方法是调用对象的依赖关系?
例如:

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”never javadoc是here

 
 
Code问答: http://codewenda.com/topics/python/
Stackoverflow: How to verify that a specific method was not called using Mockito?

*转载请注明本文链接以及stackoverflow的英文链接

发表评论

电子邮件地址不会被公开。 必填项已用*标注

+ 6 = 15