Mokito When/Then 用法

概述

本文用大量的例子和用例来阐述如何用Mockito来配置一些调用行为,本文的组织形式偏向于用法的举例与实践,将不对额外的细节做介绍。

如果你想查看其它的使用Mockito测试的文章,请参见Mockito

首先我们仍然要和上一篇文章一样Mock一个简单的list:

1
2
3
4
5
6
7
8
9
10
11
public class MyList extends AbstractList<String> {
@Override
public String get(final int index) {
return null;
}
@Override
public int size() {
return 1;
}
}

用法

为Mock对象配置简单的返回值

1
2
3
4
5
6
7
8
@Test
public final void whenMockReturnBehaviorIsConfigured_thenBehaviorIsVerified() {
final MyList listMock = Mockito.mock(MyList.class);
when(listMock.add(anyString())).thenReturn(false);
final boolean added = listMock.add(randomAlphabetic(6));
assertThat(added, is(false));
}

另一种配置返回值的方法

1
2
3
4
5
6
7
8
@Test
public final void whenMockReturnBehaviorIsConfigured2_thenBehaviorIsVerified() {
final MyList listMock = Mockito.mock(MyList.class);
doReturn(false).when(listMock).add(anyString());
final boolean added = listMock.add(randomAlphabetic(6));
assertThat(added, is(false));
}

为mock对象的指定方法配置抛出异常

1
2
3
4
5
6
7
@Test(expected = IllegalStateException.class)
public final void givenMethodIsConfiguredToThrowException_whenCallingMethod_thenExceptionIsThrown() {
final MyList listMock = Mockito.mock(MyList.class);
when(listMock.add(anyString())).thenThrow(IllegalStateException.class);
listMock.add(randomAlphabetic(6));
}

为返回值为空的方法配置行为(抛出异常)

1
2
3
4
5
6
7
@Test(expected = NullPointerException.class)
public final void whenMethodHasNoReturnType_whenConfiguringBehaviorOfMethod_thenPossible() {
final MyList listMock = Mockito.mock(MyList.class);
doThrow(NullPointerException.class).when(listMock).clear();
listMock.clear();
}

为多次调用配置行为,第一次调用不抛出异常

1
2
3
4
5
6
7
@Test
public final void givenBehaviorIsConfiguredToThrowExceptionOnSecondCall_whenCallingOnlyOnce_thenNoExceptionIsThrown() {
final MyList listMock = Mockito.mock(MyList.class);
when(listMock.add(anyString())).thenReturn(false).thenThrow(IllegalStateException.class);
listMock.add(randomAlphabetic(6));
}

第二次调用时抛出异常

1
2
3
4
5
6
7
8
@Test(expected = IllegalStateException.class)
public final void givenBehaviorIsConfiguredToThrowExceptionOnSecondCall_whenCallingTwice_thenExceptionIsThrown() {
final MyList listMock = Mockito.mock(MyList.class);
when(listMock.add(anyString())).thenReturn(false).thenThrow(IllegalStateException.class);
listMock.add(randomAlphabetic(6));
listMock.add(randomAlphabetic(6));
}

配置mock对象调用真实的方法

1
2
3
4
5
6
7
@Test
public final void whenMockMethodCallIsConfiguredToCallTheRealMethod_thenRealMethodIsCalled() {
final MyList listMock = Mockito.mock(MyList.class);
when(listMock.size()).thenCallRealMethod();
assertThat(listMock.size(), equalTo(1));
}

为mock方法配置自定义的回答

1
2
3
4
5
6
7
8
@Test
public final void whenMockMethodCallIsConfiguredWithCustomAnswer_thenRealMethodIsCalled() {
final MyList listMock = Mockito.mock(MyList.class);
doAnswer(invocation -> "Always the same").when(listMock).get(anyInt());
final String element = listMock.get(1);
assertThat(element, is(equalTo("Always the same")));
}

总结

目前列举这些例子,如果有用到新的场景会继续添加,例子的代码可以参见github

请我喝杯咖啡吧!