在单元测试中,为了隔离外部资源,使用mock构造一个虚拟对象,保证测试的目标函数能正常运行。有很多Mock方式,Mockito是其中比较通用的。
基本mock
通常有两种方式引入mockito来进行mock:
- 注解
@RunWith(MockitoJUnitRunner.class)
public class TestMockito {
@Mock
private AbstractConsumer abstractConsumer;
@Test
public void testMock() {
abstractConsumer.execute("");
}
}
- 代码方式
public class TestMockito2 {
@Mock
private AbstractConsumer abstractConsumer;
@Before
public void init() {
MockitoAnnotations.initMocks(this);
}
@Test
public void test() {
abstractConsumer.execute("");
}
}
- 当Mock对象调用方法返回对象也需要Mock时,可以使用@Mock注解中answer变量
public class DeepMockTest {
//通过answer方式可以完成深度mock
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
public IndexController indexController;
@Before
public void init() {
MockitoAnnotations.initMocks(this);
}
@Test
public void deepTest() {
indexController.helloStudent("").toString();
}
}
stub
在单元测试中,如果想调用某些方法时,想预先设定返回结果,可以通过stub来实现。
- 调用mock对象中有返回值时,录制预期结果、调用、验证调用
@RunWith(MockitoJUnitRunner.class)
public class StubbingTest {
public List list;
@Before
public void init() {
list = mock(List.class);
}
/**
* 对调用有返回值的函数调用stub预期结果
*/
@Test
public void test() {
//stub
when(list.get(0)).thenReturn("first");
//验证
assertThat(list.get(0),equalTo("first"));
//stub anyInt , then throw exception
when(list.get(anyInt())).thenThrow(new RuntimeException());
try{
list.get(0);
fail();
} catch (Exception e) {
assertThat(e, instanceOf(RuntimeException.class));
}
}
@After
public void destory() {
reset(list);
}
}
- 调用mock对象中无返回值的方法时,如何验证是否调用、验证抛出异常
@Test
public void noReturnStubTest() {
//什么都不做
doNothing().when(list).clear();
//调用
list.clear();
//验证是否执行过一次
verify(list,times(1)).clear();
//调用无返回值clear时抛出异常
doThrow(RuntimeException.class).when(list).clear();
try {
list.clear();
fail();
} catch (Exception e) {
assertThat(e, instanceOf(RuntimeException.class));
}
//验证是否调用过
verify(list,times(2)).clear();
}
- 调用mock对象有返回值的函数,调用多次时,可以预设置每一次返回的结果
/**
* stub相同方法的不同调用次数时的返回值
*/
@Test
public void MultiCallReturnMethodStubTest() {
when(list.size()).thenReturn(1,2,3,4);
assertThat(list.size(),equalTo(1));
assertThat(list.size(),equalTo(2));
assertThat(list.size(),equalTo(3));
assertThat(list.size(),equalTo(4));
}
- 调用mock对象由参数的方法时,需要根据参数来返回不同的返回值,需要thenanswer来完成
@Test
public void MultiCallArgMethodReturnStubTest() {
when(list.get(anyInt())).thenAnswer(invocation -> {
Integer argument = invocation.getArgument(0);
return String.valueOf(argument * 10);
});
assertThat(list.get(1),equalTo("10"));
assertThat(list.get(99),equalTo("990"));
}
- 调用mock对象时可以stub预期结果,可以调用到mock的源对象本身方法,通过thenCallRealMethod
@Test
public void callRealMethodTest() {
//mock是通过cglib生成代理对象
Service service = mock(Service.class);
//调用mock方法时stub返回值
when(service.callMock()).thenReturn("mock123");
assertThat(service.callMock(),equalTo("mock123"));
//调用mock对象本身的方法
when(service.callReal()).thenCallRealMethod();
assertThat(service.callReal(),equalTo(10));
}
spy
spy也是对目标对象进行mock,但是只有设置了stub的方法才会mock,其他方法直接调用目标对象本身的方法,有两种方式代码方式和annotation方式。
- 代码方式
@RunWith(MockitoJUnitRunner.class)
public class SpyTest {
@Test
public void spyTest() {
//创建实例对象
List<Integer> realList = new ArrayList<>();
//spy包装实例对象
List<Integer> list = spy(realList);
//对spy对象设置stub
when(list.isEmpty()).thenReturn(true);
when(list.size()).thenReturn(10);
//调用realList中的真实方法
list.add(1);
list.add(2);
assertThat(list.get(0),equalTo(1));
assertThat(list.get(1),equalTo(2));
assertThat(list.size(),equalTo(10));
assertThat(list.isEmpty(),equalTo(true));
}
}
- 注解方式
public class SpyAnnotationTest {
@Spy
public List<Integer> list = new ArrayList<>();
@Before
public void init() {
MockitoAnnotations.initMocks(this);
}
@Test
public void test() {
when(list.size()).thenReturn(1);
when(list.isEmpty()).thenReturn(true);
list.add(1);
list.add(2);
assertThat(list.get(0),equalTo(1));
assertThat(list.get(1),equalTo(2));
assertThat(list.isEmpty(),equalTo(true));
assertThat(list.size(),equalTo(1));
}
}
网友评论