如何验证使用Mockito不调用特定方法? [英] How to verify that a specific method was not called using Mockito?

查看:135
本文介绍了如何验证使用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 /从不,以及从不 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不调用特定方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆