【Spring】Junit加载Spring容器作单元测试(整理)
【Spring】Junit加载Spring容器作单元测试
> 引入相关Jar包
一、均需引入所需的包

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.2.10.RELEASE</version>
</dependency>

>配置文件加载方式
(a)加载配置文件<原始的手动加载方式>
ApplicationContext context = new FileSystemXmlApplicationContext("WebRoot/WEB-INF/applicationContext.xml");
new ClassPathXmlApplicationContext("applicationContext.xml");// 从classpath中加载
new FileSystemXmlApplicationContext("classpath:地址");// 没有classpath表示当前目录
(b)注解的方式自动加载方式
@org.springframework.test.context.ContextConfiguration(locations={"file:WebRoot/WEB-INF/applicationContext.xml"})
@org.springframework.test.context.ContextConfiguration(locations={"classpath:applicationContext.xml"})
@RunWith(SpringJUnit4ClassRunner.class) //使用junit4进行测试
@ContextConfiguration
({"/spring/app*.xml","/spring/service/app*.xml"}) //加载配置文件 //------------如果加入以下代码,所有继承该类的测试类都会遵循该配置,也可以不加,在测试类的方法上///控制事务,参见下一个实例
//这个非常关键,如果不加入这个注解配置,事务控制就会完全失效!
//@Transactional
//这里的事务关联到配置文件中的事务控制器(transactionManager = "transactionManager"),同时//指定自动回滚(defaultRollback = true)。这样做操作的数据才不会污染数据库!
//@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
//------------
public class BaseJunit4Test {
}
>原始的用法
测试类中要设置加载哪些Spring的配置(我这里是“/config/application*.xml”),然后就可以注入容器中的bean了。这里列举用注解的方式

package com.nicchagil.mybatis3spring3intg.junit; import java.util.List; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.nicchagil.mybatis3spring3intg.bean.User;
import com.nicchagil.mybatis3spring3intg.service.UserService; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"/config/application*.xml"})
public class JunitTest { @Autowired
private UserService userService; @Test
public void c1() {
List<User> userList = userService.query(new User());
System.out.println(userList);
} }

在具体的测试类中,采用手动加载配置文件的方式进行JUnit测试,有如下缺点:
1)导致多次Spring容器初始化问题
2)需要使用硬编码方式手工获取Bean ,需要强制转换
3)数据库现场容易遭受破坏(理想的状态:自动回滚对数据库的操作,保证数据库的现场不被破坏,因此重复测试不会发生问题)
4)不方便对数据操作正确性进行检查(理想状态:过jdbcTemplate在同一事务中访问数据库,查询数据的变化,验证操作的正确性)
> 常见的用法
常用的方式是将加载配置的部分公用出来:

package com.nicchagil.mybatis3spring3intg.junit; import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"/config/application*.xml"})
public class BaseJunit { }

然后需要的各个测试类继承公用类:
package com.nicchagil.mybatis3spring3intg.junit; import java.util.List; import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired; import com.nicchagil.mybatis3spring3intg.bean.User;
import com.nicchagil.mybatis3spring3intg.service.UserService; public class UserServiceTest extends BaseJunit { @Autowired
private UserService userService;
@Resource //自动注入,默认按名称
private IBaseDao baseDao; @Test //标明是测试方法
@Transactional //标明此方法需使用事务
@Rollback(false) //标明使用完此方法后事务不回滚,true时为回滚
public void c1() {
List<User> userList = userService.query(new User());
System.out.println(userList);
}
}
//复习:@Autowired & @Resource 的区别
//@Autowired注解是按类型装配依赖对象,默认情况下它要求依赖对象必须存在,如果允许null值,可以设置它required属性为false。如果我们想使用按名称装配,可以结合@Qualifier注解一起使用。如下:
// @Autowired @Qualifier("personDaoBean")
// private PersonDao personDao; //@Resource注解和@Autowired一样,也可以标注在字段或属性的setter方法上,但它默认按名称装配。名称可以通过@Resource的name属性指定,如果没有指定name属性,当注解标注在字段上,即默认取字段的名称作为bean名称寻找依赖对象,当注解标注在属性的setter方法上,即默认取属性名作为bean名称寻找依赖对象。
// @Resource(name=“personDaoBean”)
// private PersonDao personDao;//用于字段上 //注意:如果没有指定name属性,并且按照默认的名称仍然找不到依赖对象时, @Resource注解会回退到按类型装配。但一旦指定了name属性,就只能按名称装配了。
【Spring】Junit加载Spring容器作单元测试(整理)的更多相关文章
- 【Spring】Junit加载Spring容器作单元测试
如果我们需要对我们的Service方法作单元测试,恰好又是用Spring作为IOC容器的,我们可以这么配置Junit加载Spring容器,方便做单元测试. > 基本的搭建 (1)引入所需的包 & ...
- Junit加载Spring容器作单元测试
阅读目录 > 基本的搭建 > 常见的用法 如果我们需要对我们的Service方法作单元测试,恰好又是用Spring作为IOC容器的,我们可以这么配置Junit加载Spring容器,方便做单 ...
- spring boot 加载web容器tomcat流程源码分析
spring boot 加载web容器tomcat流程源码分析 我本地的springboot版本是2.5.1,后面的分析都是基于这个版本 <parent> <groupId>o ...
- junit测试用例加载spring配置文件
junit加载pom引用项目的xml配置文件,如果定义了<beans profile="dev">,必须在测试用例类上面加上标记 @ActiveProfiles(&qu ...
- 加载spring容器
import org.springframework.context.ApplicationContext; import org.springframework.context.support.Cl ...
- Spring Boot中采用Mockito来mock所测试的类的依赖(避免加载spring bean,避免启动服务器)
最近试用了一下Mockito,感觉真的挺方便的.举几个应用实例: 1,需要测试的service中注入的有一个dao,而我并不需要去测试这个dao的逻辑,只需要对service进行测试.这个时候怎么办呢 ...
- Spring中加载xml配置文件的六种方式
Spring中加载xml配置文件的六种方式 博客分类: Spring&EJB XMLSpringWebBeanBlog 因为目前正在从事一个项目,项目中一个需求就是所有的功能都是插件的形式装 ...
- Spring中加载配置文件的方式
原文:http://blog.csdn.net/snowjlz/article/details/8158560 Spring 中加载XML配置文件的方式,好像有3种, XML是最常见的Spring 应 ...
- Web.xml配置详解之context-param (加载spring的xml,然后初始化bean看的)
http://www.cnblogs.com/goody9807/p/4227296.html(很不错啊) 容器先加载spring的xml,然后初始化bean时,会为bean赋值,包括里面的占位符
随机推荐
- [nowcoder]青蛙
链接:https://www.nowcoder.com/acm/contest/158/F 挺有意思的一道题,考场并查集忘记路径压缩就没AK== 很显然一个贪心是不,每只青蛙使劲往前跳,能跳多远跳多远 ...
- 什么是Zero-Copy?
概述 考虑这样一种常用的情形:你需要将静态内容(类似图片.文件)展示给用户.那么这个情形就意味着你需要先将静态内容从磁盘中拷贝出来放到一个内存buf中,然后将这个buf通过socket传输给用户,进而 ...
- LeetCode——Insertion Sort List
LeetCode--Insertion Sort List Question Sort a linked list using insertion sort. Solution 我的解法,假设第一个节 ...
- eclipse显示结果窗口字体大小
设置前的字体大小 设置后的字体大小 步骤
- SpringBoot配置文件 application.properties详解
SpringBoot配置文件 application.properties详解 本文转载:https://www.cnblogs.com/louby/p/8565027.html 阅读过程中若发现 ...
- VS路径定义
你可以在项目“属性页”对话框中接受字符串的任意位置使用这些宏. 这些宏不区分大小写. 若要显示当前可用的宏,请在属性名称右侧列中单击下拉箭头. 如果“编辑”可用,请单击它,然后在“编辑”对话框中单击“ ...
- scala学习手记33 - 使用trait进行装饰
在上一节看到了scala的在实例一级的选择性混入就不得不感叹scala在语法上的扩展性.就通过这样一个特性scala简化了很多在java中的编程概念和设计模式. 比如说在java中常用的组合,以及装饰 ...
- Spring Boot的核心
1.1.1. 入口类和@SpringBootApplication Spring Boot的项目一般都会有*Application的入口类,入口类中会有main方法,这是一个标准的Java应用程序 ...
- POST方式跨域上传文件
JSONP请求有限制: 第一,不能跳出两层, 第二,不支持POST. 往往解决跨域POST请求的方案是个"古老"方法, 请求同域下的iframe. 服务器端: 需要附加头信息: ...
- S3C2440启动方式
不管S3C2440的启动设备是什么,它都是从0x0000 0000地址开始执行程序的,所不同的是地址的映射不一样.基于S3C2440的嵌入式系统上电之后,需要首选选择启动设备,2440的启动方式选择是 ...