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

本文翻译自:How to verify that a specific method was not called using Mockito?

How to verify that a method is not called on an object's dependency? 如何验证对对象的依赖项调用方法?

For example: 例如:

public interface Dependency {
    void someMethod();
}

public class Foo {
    public bar(final Dependency d) {
        ...
    }
}

With the Foo test: 通过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??**
    }
}

#1楼

参考:https://stackoom.com/question/ryAF/如何验证未使用Mockito调用特定方法


#2楼

Mockito.verify方法上使用第二个参数,如下所示:

verify(dependency, Mockito.times(0) ).someMethod()


#3楼

Even more meaningful : 更有意义的是:

import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;

// ...

verify(dependency, never()).someMethod();

The documentation of this feature is there §4 "Verifying exact number of invocations / at least x / never" , and the never javadoc is here . 有关此功能的文档,请参见§4“验证确切的调用次数/至少x / never” ,并且never Javadoc在此处


#4楼

As a more general pattern to follow, I tend to use an @After block in the test: 作为更通用的模式,我倾向于在测试中使用@After块:

@After
public void after() {
    verifyNoMoreInteractions(<your mock1>, <your mock2>...);
}

Then the test is free to verify only what should be called. 这样,测试就可以自由地仅验证调用的内容。

Also, I found that I often forgot to check for "no interactions", only to later discover that things were being called that shouldn't have been. 另外,我发现我经常忘记检查“无交互作用”,只是后来发现发现本来不应该进行的调用。

So I find this pattern useful for catching all unexpected calls that haven't specifically been verified. 因此,我发现这种模式对于捕获所有未经特别验证的意外呼叫很有用。


#5楼

Both the verifyNoMoreInteractions() and verifyZeroInteracions() method internally have the same implementation as: verifyNoMoreInteractions()verifyZeroInteracions()方法在内部具有与以下相同的实现:

public static transient void verifyNoMoreInteractions(Object mocks[])
{
    MOCKITO_CORE.verifyNoMoreInteractions(mocks);
}

public static transient void verifyZeroInteractions(Object mocks[])
{
    MOCKITO_CORE.verifyNoMoreInteractions(mocks);
}

so we can use any one of them on mock object or array of mock objects to check that no methods have been called using mock objects. 因此我们可以在模拟对象或模拟对象数组上使用它们中的任何一个,以检查是否没有使用模拟对象调用任何方法。


#6楼

First of all: you should always import mockito static, this way the code will be much more readable (and intuitive): 首先:您应该始终导入Mockito static,这样,代码将更具可读性(直观):

import static org.mockito.Mockito.*;

There are actually many ways to achieve this, however it's (arguably) cleaner to use the 实际上有很多方法可以实现这一目标,但是(可以说)使用

verify(yourMock, times(0)).someMethod();

method all over your tests, when on other Tests you use it to assert a certain amount of executions like this: 方法遍及您的所有测试,在其他测试上使用时,您可以使用它来声明一定数量的执行,如下所示:

verify(yourMock, times(5)).someMethod();

Alternatives are: 替代方法是:

verify(yourMock, never()).someMethod();

Alternatively - when you really want to make sure a certain mocked Object is actually NOT called at all - you can use: 或者,当您确实要确保完全不调用某个模拟对象时,可以使用:

verifyZeroInteracions(yourMock)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值