使用spring aop 简化mock设计实现

有时候为了去掉对外部系统的依赖, 我们需要针对外部依赖的接口创建一个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, 还是有一定参考意义的.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值