本文翻译自: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)