PowerMock+SpringMVC整合并测试Controller层方法
PowerMock扩展自Mockito,实现了Mockito不支持的模拟形式的单元测试。PowerMock实现了对静态方法、构造函数、私有方法以及final方法的模拟支持,对静态初始化过程的移除等强大的功能。(官方)
接下来,通过实例来实现对SpringMVC的Controller层的方法模拟测试。
一、maven引入PowerMock相应的Jar文件
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.6.</version>
</dependency> <dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.6.</version>
</dependency>
二、单元测试基类
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional; @RunWith(PowerMockRunner.class) //使用PowerMockRunner运行时
@PowerMockRunnerDelegate(SpringJUnit4ClassRunner.class) //委派给SpringJUnit4ClassRunner
@ContextConfiguration (locations={"classpath:META-INF/app_config/context/context-*.xml"}) //加载配置文件
@Transactional
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
@PowerMockIgnore({"javax.management.*"}) //忽略一些mock异常
public class BaseJunit4 { }
@PowerMockRunnerDelegate(SpringJUnit4ClassRunner.class):PowerMock与Spring整合时最重要的配置
@TransactionConfiguration(transactionManager="transactionManager",defaultRollback =true): 这里的事务关联到项目的配置文件中的事务控制器("transactionManager"),同时指定事物自动回滚(defaultRollback= true),以此对数据库不会产生数据污染。
三、单元测试类
对Controller测试时,还需要由Controller层中的Action去执行相应的业务代码,对于Action的依赖注入就可以非常方便的使用ReflectionTestUtils.setField(controller,"assetScrapHandleAction",aSHAction)(非常重要),将aSHAction注入到controller层中的属性名为assetScrapHandleAction中
@PrepareForTest({OrgUtils.class,UserUtils.class}) 注解一定要放到类上,或者基类上也可以,千万不能放到方法上。如果在方法上,会导致每执行一次test方法,都会委托SpringJUnit4ClassRunner去加载一次上下文配置,将直接导致内存耗尽。
四、Controller层代码实例
UserUtils.getLoginUserCom().getLeavel();执行时,返回“L0”
UserUtils.getUser().getUser().getOrgCode();执行时,返回“1100”
这些都是PowerMock的功劳!
PowerMock+SpringMVC整合并测试Controller层方法的更多相关文章
- Junit mockito 测试Controller层方法有Pageable异常
1.问题 在使用MockMVC+Mockito模拟Service层返回的时候,当我们在Controller层中参数方法调用有Pageable对象的时候,我们会发现,我们没办法生成一个Pageable的 ...
- springMVC中controller层方法中使用private和public问题
楼主一直习惯使用public,偶尔手误也可能使用private,但是发觉也没啥区别,都能调用service层,注入bean. 后来做一个新项目时,发觉自己以前的写的部分功能报错,当时有点懵逼,,找了半 ...
- 【异常处理】Springboot对Controller层方法进行统一异常处理
Controller层方法,进行统一异常处理 提供两种不同的方案,如下: 方案1:使用 @@ControllerAdvice (或@RestControllerAdvice), @ExceptionH ...
- Springboot对Controller层方法进行统一异常处理
Controller层方法,进行统一异常处理 提供两种不同的方案,如下: 方案1:使用 @@ControllerAdvice (或@RestControllerAdvice), @ExceptionH ...
- 代理模式——用AOP测试业务层方法的执行时间
代理模式 对代理模式的理解,通过http://www.runoob.com/design-pattern/proxy-pattern.html 对AOP的代理模式,参考https://www.cnbl ...
- SpringBoot测试Controller层
一.准备工作 1.导入测试依赖 <dependency> <groupId>org.springframework.boot</groupId> <artif ...
- spring security 在controller层 方法级别使用注解 @PreAuthorize("hasRole('ROLE_xxx')")设置权限拦截 ,无权限则返回403
1.前言 以前学习的时候使用权限的拦截,一般都是对路径进行拦截 ,要么用拦截器设置拦截信息,要么是在配置文件内设置拦截信息, spring security 支持使用注解的形式 ,写在方法和接口上拦截 ...
- Springmvc入门基础(五) ---controller层注解及返回类型解说
0.@Controller注解 作用:通过@Controller注解,注明该类为controller类,即控制器类,需要被spring扫描,然后注入到IOC容器中,作为Spring的Bean来管理,这 ...
- junit基础学习之-测试controller层(2)
准备工作: eclipse本身带有junit4,可以直接build path,加入junit. 连接数据库的配置文件需要修改,之前的文件是采用properties+xml文件的形式,但是在测试的时候因 ...
随机推荐
- D. Vitya and Strange Lesson Codeforces Round #430 (Div. 2)
http://codeforces.com/contest/842/problem/D 树 二进制(路径,每个节点代表一位) #include <cstdio> #include < ...
- 团体程序设计天梯赛 L3-004. 肿瘤诊断
数组的大小不能开太大,否则会出现段错误 用bfs而不用dfs,dfs存储太多中间过程,会超内存 #include <stdio.h> #include <stdlib.h> # ...
- 科学计算三维可视化---TraitsUI与Mayavi实例
TraitsUI与Mayavi实例 一:创建一个简单的TraitsUI与Mayavi实例 from numpy import sqrt,sin,mgrid from traits.api import ...
- linux网卡的开启
一:文件配置网卡在开机时,自动启用 首先我们使用 ip addr查看IP信息 [root@redhat2 network-scripts]# ip addr : lo: <LOOPBACK,UP ...
- Mongodb 笔记02 创建、更新和删除文档
创建.更新和删除文档 1. 插入并保存: 1). 单条插入,insert : db.foo.insert({"bar":"baz"}) 2). ...
- vuejs实现数据驱动视图原理
什么是数据驱动 数据驱动是vuejs最大的特点.在vuejs中,所谓的数据驱动就是当数据发生变化的时候,用户界面发生相应的变化,开发者不需要手动的去修改dom. 比如说我们点击一个button,需要元 ...
- windows之tracert命令
tracert命令是使用从本地到目标网站所在网络服务器的一系列网络节点的访问速度, 网络节点最多支持显示30个.命令格式是tracert加空格加目标网站名称(也可以输入目标网站的IP地址). 先以百度 ...
- PHP7 学习笔记(二)PHP5.9 升级到PHP7 遇到的一些坑的记录(php-fpm 图解)
apache_event_php-fpm 示意图: nginx-php-fpm示意图: Worker-Master-Server TCP-Nginx_PHP Nginx-FastCGI 1.使用$_G ...
- uploadify IE11 不兼容问题(不显示图片)
1.进入uploadify官网demo : http://www.uploadify.com/demos/ 2. 显示 (确认flash为最新版本) 3.更换其它浏览器一切正常 4.原因:I ...
- linux 系统下IntelliJ IDEA的安装及使用
由于刚刚进入研究生阶段,通过几个月对大数据的学习,从java到hadoop,再到scala到spark.在这我写一下我在ubuntu系统下intelliJ IDEA的安装和配置.首先我的ubuntu系 ...