1.关键词
单元测试、spring、mockito
2.概述
单元测试目前已经成为项目中保证代码质量的一种必要方法,对于一些不易构造或者不易获取的对象通过mock的方式进行测试是一种很有效的处理办法。在基于spring的mock测试中,mock对象获取和使用的便利性可以提升单元测试代码的质量。
3.实现原理
Mock对象的注入使用注解和反射,对象注入依赖spring框架支持junit4提供的TestExcutionListeners监听器对象,在监听器中将mock对象注入到单元测试类中。
4.新建对象方式代码
private IAccessServiceaccessService = Mockito.mock(IAccessService.class); @BeforeClass public static void beforeClass(){ // 构造并注入Mock对象ICmsProxyService sceneConfigService.setAccessService(accessService); } |
5.监听器方式代码
5.1 实现监听器
继承DependencyInjectionTestExecutionListener类,
实现injectDependencies(TestContexttestContext)方法
public class MockitoDependencyInjectionTestExecutionListener extends DependencyInjectionTestExecutionListener { @Override protected void injectDependencies(TestContext testContext) throws Exception { super.injectDependencies(testContext); init(testContext); } |
5.2 利用反射注入mock对象
private void init(final TestContext testContext)throws Exception { Object bean = testContext.getTestInstance(); Field[] fields = bean.getClass().getDeclaredFields(); for (Field field : fields) { Annotation[] annotations = field.getAnnotations(); for (Annotation annotation : annotations) { if(annotationinstanceof Mock){ //注入Mock实例 MockObject obj = new MockObject(); obj.setType(field.getType()); obj.setObj(Mockito.mock(field.getType())); field.setAccessible(true); field.set(bean, obj.getObj()); mockObjectMap.put(field.getName(), obj); }else if(annotation instanceofAutowired){ injectFields.add(field); } } } |