Mockito Verify Cookbook – Mockito验证食谱

最后修改: 2013年 11月 6日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

This cookbook illustrates how to use Mockito verify in a variety of use cases.

本手册说明了如何在各种用例中使用Mockito验证

The format of the cookbook is example-focused and practical — no extraneous details and explanations necessary.

烹饪书的格式是以实例为中心的和实用的–没有多余的细节和解释的必要。

We’re going to be mocking a simple list implementation:

我们将模拟一个简单的列表实现。

public class MyList extends AbstractList<String> {

    @Override
    public String get(final int index) {
        return null;
    }
    @Override
    public int size() {
        return 0;
    }
}

2. The Cookbook

2.食谱

Verify simple invocation on mock:

验证对模拟的简单调用:

List<String> mockedList = mock(MyList.class);
mockedList.size();
verify(mockedList).size();

Verify number of interactions with mock:

验证与模拟的互动次数:

List<String> mockedList = mock(MyList.class);
mockedList.size();
verify(mockedList, times(1)).size();

Verify no interaction with the whole mock occurred:

验证没有发生与整个模拟的互动:

List<String> mockedList = mock(MyList.class);
verifyNoInteractions(mockedList);

Verify no interaction with a specific method occurred:

验证没有发生与特定方法的互动:

List<String> mockedList = mock(MyList.class);
verify(mockedList, times(0)).size();

Verify there are no unexpected interactions — this should fail:

验证是否有意外的互动 – 这应该是失败的:

List<String> mockedList = mock(MyList.class);
mockedList.size();
mockedList.clear();
verify(mockedList).size();
verifyNoMoreInteractions(mockedList);

Verify order of interactions:

验证互动的顺序:

List<String> mockedList = mock(MyList.class);
mockedList.size();
mockedList.add("a parameter");
mockedList.clear();

InOrder inOrder = Mockito.inOrder(mockedList);
inOrder.verify(mockedList).size();
inOrder.verify(mockedList).add("a parameter");
inOrder.verify(mockedList).clear();

Verify an interaction has not occurred:

验证一个互动没有发生:

List<String> mockedList = mock(MyList.class);
mockedList.size();
verify(mockedList, never()).clear();

Verify an interaction has occurred at least a certain number of times:

验证一个互动至少已经发生了一定的次数:

List<String> mockedList = mock(MyList.class);
mockedList.clear();
mockedList.clear();
mockedList.clear();

verify(mockedList, atLeast(1)).clear();
verify(mockedList, atMost(10)).clear();

Verify interaction with exact argument:

验证与确切参数的互动:

List<String> mockedList = mock(MyList.class);
mockedList.add("test");
verify(mockedList).add("test");

Verify interaction with flexible/any argument:

验证与灵活/任何参数的互动:

List<String> mockedList = mock(MyList.class);
mockedList.add("test");
verify(mockedList).add(anyString());

Verify interaction using argument capture:

使用参数捕获验证互动:

List<String> mockedList = mock(MyList.class);
mockedList.addAll(Lists.<String> newArrayList("someElement"));

ArgumentCaptor<List<String>> argumentCaptor = ArgumentCaptor.forClass(List.class);
verify(mockedList).addAll(argumentCaptor.capture());

List<String> capturedArgument = argumentCaptor.getValue();
assertThat(capturedArgument).contains("someElement");

3. Conclusion

3.结论

The goal of this guide is to have this information readily available online. I’ve published a few similar development cookbooks on Google Guava and Hamcrest.

本指南的目标是让这些信息可以在网上随时获得。我已经在Google GuavaHamcrest上发表了一些类似的开发食谱。

The implementation of all these examples and code snippets can be found over on GitHub. This is a Maven-based project, so it should be easy to import and run as it is.

所有这些例子和代码片段的实现都可以在GitHub上找到over on GitHub。这是一个基于Maven的项目,所以应该很容易导入并按原样运行。