有时候为了去掉对外部系统的依赖, 我们需要针对外部依赖的接口创建一个mock实现类, 当然里面都是空实现, 如果有多个的话, 可能需要写很多的mock实现类, 我们在测试的时候, 发现可以通过spring aop来进行简化, 使用一个aspect来替代多个mock实现. 具体做法如下:
首先是aop的配置文件, 如下:
<?xml version="1.0" encoding="GBK"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"
default-autowire="byName">
<!-- aop 拦截器配置 -->
<aop:aspectj-autoproxy />
<bean id="mockAspect" class="com.mysoft.mock.aspect.MockAspect" lazy-init="false">
</bean>
<aop:config>
<aop:aspect ref="mockAspect">
<aop:around pointcut="execution(* com.mysoft.manager.impl.DefaultCategoryManager.*(. .))" method="invokeOfCategory"/>
<aop:around pointcut="execution(* com.mysoft.manager.PropertyManager.*(. .))" method="invokeAndReturnNull"/>
<aop:around pointcut="execution(* com.mysoft.manager.impl.TxtFileManager.*(. .))" method="invokeAndReturnNull"/>
<aop:around pointcut="execution(* com.mysoft.manager.KeywordsChecker.checkNormalKeywords(. .))" method="invokeAndReturnString"/>
<aop:around pointcut="execution(* com.mysoft.manager.KeywordsChecker.checkFixKeywords(String, long)) and args(text, categoryId)" method="checkFixKeywords" arg-names="join,text, categoryId"/>
</aop:aspect>
</aop:config>
这里主要配置了一个用来放实际执行的方面类:MockAspect, 然后是一些pointcut的表达式配置.也就是告诉spring aop, 当碰到某个类, 某个方法的时候, 去执行MockAspect中对应的某个方法.
MockAspect的内容如下:
public class MockAspect implements BeanFactoryAware {
private BeanFactory beanFactory;
public Object invokeOfCategory(ProceedingJoinPoint join) throws Throwable {
CategoryManager categoryManager = (CategoryManager) beanFactory.getBean("categoryManagerMock");
MethodInvocationProceedingJoinPoint call = (MethodInvocationProceedingJoinPoint) join;
Signature signature = call.getSignature();
MethodSignature ms = (MethodSignature) signature;
String methodName = signature.getName();
Method method = categoryManager.getClass().getMethod(methodName, ms.getParameterTypes());
return method.invoke(categoryManager, join.getArgs());
}
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
}
/**
*
* 一个默认的处理, 什么也不做
*
* @param join
* @return
* @throws Throwable
*/
public Object invokeAndReturnNull(ProceedingJoinPoint join) throws Throwable {
return null;
}
public String invokeAndReturnString(ProceedingJoinPoint join) throws
Throwable {
return "";
}
/**
* 对{@link String
com.mysoft.manager.KeywordsChecker#checkFixKeywords(String,
long)}方法进行切面
* @param join
* @param text
* @param categoryId
* @return
* @throws Throwable
*/
public String checkFixKeywords(ProceedingJoinPoint join, String text,
long categoryId)
throws Throwable {
if ("abc".equals(text)) {
return "def";
}
return null;
}
}
这里还需要提到的一个东东, 就是实现了BeanFactoryAware 接口, 主要是为了在一起运行所有的test case的时候, 有时候里面有几个bean需要替换为mock的实现类(比如这里的categoryManagerMock), 这里我们做了一个switch的动作, 让他不是去调用具体的类, 而是我们定义的一个mock实现类.
当然这种做法也不要滥用, 适可而止, 针对一些简单的, 大量的mock, 还是有一定参考意义的.
通过Spring AOP简化对外部系统的依赖mock实现,利用Aspect减少mock类编写。配置特定切点,执行对应方法,如返回null或特定值。
540

被折叠的 条评论
为什么被折叠?



