通过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>
参考
spring-aop-aspect-not-working-using-mockito
mockito-and-spring-proxies
is-it-possible-to-unproxy-a-spring-bean
通过AopTestUtils对切面对象进行mock的更多相关文章
- 面向切面对象AOP
前言 面向切面编程(思想)AOP Aspect Oriented Programming,是面向对象基础上 更关注最终目标 而不关注中间的小目标,简而言之,就是我们的目标(例如constroller ...
- 使用模拟对象(Mock Object)技术进行测试驱动开发
敏捷开发 敏捷软件开发又称敏捷开发,是一种从上世纪 90 年代开始引起开发人员注意的新型软件开发方法.和传统瀑布式开发方法对比,敏捷开发强调的是在几周或者几个月很短的时间周期,完成相对较小功能,并交付 ...
- 用PowerMock mock 由工厂方法产生的对象
有些对象需要mock的对象是由工厂方法产生出来的,而工厂方法一般是静态方法,这时候就需要同时mock工厂方法及对象 被测方法: public class EmployeeServiceFactory ...
- 基于spring与mockito单元测试Mock对象注入
转载:http://www.blogjava.net/qileilove/archive/2014/03/07/410713.html 1.关键词 单元测试.spring.mockito 2.概述 单 ...
- .Net中的AOP系列之《单元测试切面》
返回<.Net中的AOP>系列学习总目录 本篇目录 使用NUnit编写测试 编写和运行NUnit测试 切面的测试策略 Castle DynamicProxy测试 测试一个拦截器 注入依赖 ...
- .Net中的AOP系列之《方法执行前后——边界切面》
返回<.Net中的AOP>系列学习总目录 本篇目录 边界切面 PostSharp方法边界 方法边界 VS 方法拦截 ASP.NET HttpModule边界 真实案例--检查是否为移动端用 ...
- 【Python + Selenium】Mock Testing 是啥?一个so上的高票答案。
There are many kinds of testing which really made me confused. To be honest, I've never heard of som ...
- Hello Spring Framework——面向切面编程(AOP)
本文主要参考了Spring官方文档第10章以及第11章和第40章的部分内容.如果要我总结Spring AOP的作用,不妨借鉴文档里的一段话:One of the key components of S ...
- mock测试框架Mockito
无论是敏捷开发.持续交付,还是测试驱动开发(TDD)都把单元测试作为实现的基石.随着这些先进的编程开发模式日益深入人心,单元测试如今显得越来越重要了.在敏捷开发.持续交付中要求单元测试一定要快(不能访 ...
随机推荐
- Python 优雅的操作字典【转】
Python 中的字典是Python中一个键值映射的数据结构,下面介绍一下如何优雅的操作字典. 1.1 创建字典 Python有两种方法可以创建字典,第一种是使用花括号,另一种是使用内建 函数dict ...
- C#图片压缩的实现方法
一般在web应用中,对客户端提交上来的图片肯定需要进行压缩的.尤其是比较大的图片,如果不经过压缩会导致页面变的很大,打开速度比较慢,当然了如果是需要高质量的图片也得需要生产缩略图. 一般在web应用中 ...
- POJ 3494 Largest Submatrix of All 1’s(最大全1子矩阵)
题目链接:http://poj.org/problem?id=3494 题意:给出一个01的矩阵,找出一个面积最大的全1矩阵. 思路:用h[i][j]表示从位置(i,j)向上连续1的最大长度.之后枚举 ...
- 【uva】1220 Party at Hali-Bula
1. 题目描述公司里有$n, n \in [1, 200]$个人,他们间的关系构成树状结构.除老板外,每个员工都有唯一一个直属上司,要求从中选择尽量多的人,但是不能同时选择员工和他的直属上司,问最多能 ...
- 第六讲(二) Hibernate HQL查询
HQL查询:Criteria查询对查询条件进行了面向对象封装,符合编程人员的思维方式,不过HQL(Hibernate Query Lanaguage)查询提供了更加丰富的和灵活的查询特性,因此Hibe ...
- cocos2dx 坐标系 -转
(原文出处找不到了) 无论是搞2d还是3d开发,最需要搞清楚的就是坐标系,这部分混乱的话就没啥奔头了.所以玩cocos2d,一上来就先把各种与坐标有关的东西搞清楚. 基本的两个坐标系:屏幕坐标系和GL ...
- laravel named route
laravel中一般对于路由的使用方法是在routes.php中定义一个路由,在view中如果要引用一个url则直接通过<a href="url/">来使用. 但是随着 ...
- R语言AMORE包实现BP神经网络——German数据集
library(AMORE)data<-read.table('G:\\dataguru\\ML\\ML09\\基于BP网络的个人信贷信用评估\\基于BP网络的个人信贷信用评估\\german. ...
- Sencha CMD 4- 安装与首次使用
哥英文不好,网上搜索好多中文教程都是抄来抄去没有完整的介绍.所以写出来让与我一样的小伙伴惊呆下! 这篇主要是安装,后续慢慢更新 一.Sencha CMD是干啥滴!? 它是服务使用EXTJS SDK开发 ...
- [转载]hao123军事频道首页JQ焦点图
适用浏览器:IE8.360.FireFox.Chrome.Safari.Opera.傲游.搜狗.世界之窗. *本作品由[站长素材]收集整理,转载请注明出处! 下载地址