通过AopTestUtils对切面对象进行mock

概述

当对一个切面类进行测试时,由于Spring对切面对象生成了proxy对象,此时对切面对象使用ReflectionTestUtils赋值,操作的是proxy对象,而不是真实对象,会使得赋值出问题。可以通过引入AopTestUtils解决赋值问题。

AopTestUtils使用思路

通过AopTestUtils可以通过切面proxy对象,获取到切面的真实对象。通过使用ReflectionTestUtils对真实的切面对象修改依赖,到达mock的目的。

代码例子

准备切面对象:
IBar:

package com.github.yongzhizhan.draftbox.springtest.aop;

public interface IBar {
    String perform(String message);
}

Bar:

package com.github.yongzhizhan.draftbox.springtest.aop;

import org.springframework.beans.factory.annotation.Autowired;

public class Bar implements IBar {
    @Autowired
    Dep dep;

    @Override
    public String perform(final String message) {
        System.out.println("run bar " + message);
        return dep.perform("aspect");
    }
}

依赖对象:

package com.github.yongzhizhan.draftbox.springtest.aop;

/**
 * Dependence class
 * @author zhanyongzhi
 */
public class Dep {
    public String perform(String msg){
        return msg;
    }
}

切面:

package com.github.yongzhizhan.draftbox.springtest.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

/**
 * 切面
 */
@Aspect
public class BarAspect {
    @Before(value = "execution(* com.github.yongzhizhan.draftbox.springtest.aop.Bar.perform(..))")
    public void beforeSayHello(JoinPoint vJoinPoint){
        System.out.println("aspect before "+vJoinPoint.getArgs()[0]);
    }
}

测试例子

package com.github.yongzhizhan.draftbox.springtest;

import com.github.yongzhizhan.draftbox.springtest.aop.Bar;
import com.github.yongzhizhan.draftbox.springtest.aop.BarAspect;
import com.github.yongzhizhan.draftbox.springtest.aop.Dep;
import com.github.yongzhizhan.draftbox.springtest.aop.IBar;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.util.AopTestUtils;
import org.springframework.test.util.ReflectionTestUtils;

import static org.mockito.Mockito.when;

/**
 * 通过AopTestUtils解决ReflectionTestUtils赋值切面对象的问题
 * @author zhanyongzhi
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath*:**web-config.xml")
public class AopTestUtilsTest {
    @Mock
    Dep dep;

    @Autowired
    private BarAspect barAspect;

    @Autowired
    ApplicationContext applicationContext;

    @Autowired
    @InjectMocks
    IBar bar;

    @Before
    public void setUp(){
        MockitoAnnotations.initMocks(this);

        //对象默认返回aspect,修改为返回mock
        when(dep.perform("aspect")).thenReturn("mock");
    }

    @Test
    public void testDefault(){
        String tRet = bar.perform("hello");

        //mock注入无效,仍然返回aspect
        if(!"aspect".equals(tRet))
            Assert.fail("perform return not equeal aspect");
    }

    @Test
    public void testAopUtils(){

        //获取真实的代理对象
        Bar tBar = AopTestUtils.getTargetObject(bar);
        ReflectionTestUtils.setField(tBar, "dep", dep);

        String tRet = bar.perform("hello");

        //此时才真正mock到
        if(!"mock".equals(tRet))
            Assert.fail("perform return not equeal mock");
    }
}

配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context-4.1.xsd
               http://www.springframework.org/schema/mvc
               http://www.springframework.org/schema/mvc/spring-mvc.xsd
               http://www.springframework.org/schema/aop
               http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">

    <!-- scan the package and the sub package -->
    <mvc:annotation-driven/>
    <context:component-scan base-package="com.github.yongzhizhan.draftbox.springtest"/>

    <bean id="bar" class="com.github.yongzhizhan.draftbox.springtest.aop.Bar"/>
    <bean id="dep" class="com.github.yongzhizhan.draftbox.springtest.aop.Dep"/>
    <bean id="barAspect" class="com.github.yongzhizhan.draftbox.springtest.aop.BarAspect"/>

    <aop:aspectj-autoproxy/>
</beans>

在github中查看

参考

spring-aop-aspect-not-working-using-mockito
mockito-and-spring-proxies
is-it-possible-to-unproxy-a-spring-bean

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,991评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,971评论 6 342
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,769评论 18 399
  • 见到窗外里的林青霞,阳光柠檬茶广告的张柏芝,你深深的吸一口气,对她们吹一口气,变小变小变小。。。 于是他们就像金箍...
    三福弗朗西斯阅读 201评论 0 0
  • 中文 钥匙的存在由来已久。最早的钥匙可追溯到 4000 年前,由古埃及人用木头制成。对钥匙做出了一些改进,改用金属...
    西坡师妹阅读 397评论 0 0