Mockito When/Then Cookbook – Mockito When/Then Cookbook

最后修改: 2013年 11月 11日

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

1. Overview

1.概述

This cookbook shows how to use Mockito to configure behavior in a variety of examples and use cases.

本手册展示了如何在各种示例和用例中使用Mockito来配置行为

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

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

And of course, if you want to learn more about testing well with Mockito, have a look at the other Mockito articles here.

当然,如果你想了解更多关于用Mockito进行良好测试的信息,可以看看这里的其他Mockito文章

We’re going to be mocking a simple list implementation, which is the same implementation we used in the previous cookbook:

我们将模拟一个简单的列表实现,这与我们在之前的cookbook中使用的实现相同。

public class MyList extends AbstractList<String> {

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

2. The Cookbook

2.食谱

Configure simple return behavior for mock:

为mock配置简单的返回行为:

MyList listMock = Mockito.mock(MyList.class);
when(listMock.add(anyString())).thenReturn(false);

boolean added = listMock.add(randomAlphabetic(6));
assertThat(added).isFalse();

Configure return behavior for mock in an alternative way:

以另一种方式配置mock的返回行为:

MyList listMock = Mockito.mock(MyList.class);
doReturn(false).when(listMock).add(anyString());

boolean added = listMock.add(randomAlphabetic(6));
assertThat(added).isFalse();

Configure mock to throw an exception on a method call:

配置mock以在方法调用时抛出一个异常:

@Test(expected = IllegalStateException.class)
public void givenMethodIsConfiguredToThrowException_whenCallingMethod_thenExceptionIsThrown() {
    MyList listMock = Mockito.mock(MyList.class);
    when(listMock.add(anyString())).thenThrow(IllegalStateException.class);

    listMock.add(randomAlphabetic(6));
}

Configure the behavior of a method with void return type — to throw an exception:

配置一个返回类型为void的方法的行为–抛出一个异常:

MyList listMock = Mockito.mock(MyList.class);
doThrow(NullPointerException.class).when(listMock).clear();

listMock.clear();

Configure the behavior of multiple calls:

配置多次调用的行为:

MyList listMock = Mockito.mock(MyList.class);
when(listMock.add(anyString()))
  .thenReturn(false)
  .thenThrow(IllegalStateException.class);

listMock.add(randomAlphabetic(6));
listMock.add(randomAlphabetic(6)); // will throw the exception

Configure the behavior of a spy:

配置一个间谍的行为:

MyList instance = new MyList();
MyList spy = Mockito.spy(instance);

doThrow(NullPointerException.class).when(spy).size();
spy.size(); // will throw the exception

Configure method to call the real, underlying method on a mock:

配置方法,在模拟的基础上调用真实的方法:

MyList listMock = Mockito.mock(MyList.class);
when(listMock.size()).thenCallRealMethod();

assertThat(listMock).hasSize(1);

Configure mock method call with custom Answer:

用自定义Answer配置模拟方法调用:

MyList listMock = Mockito.mock(MyList.class);
doAnswer(invocation -> "Always the same").when(listMock).get(anyInt());

String element = listMock.get(1);
assertThat(element).isEqualTo("Always the same");

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 and now Mockito.

本指南的目标是让这些信息可以在网上随时获得。我已经在Google GuavaHamcrest以及now Mockito上发表过一些类似的开发手册。

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上找到。这是一个基于Maven的项目,所以它应该很容易导入和运行。