使用Spring+Junit4进行测试
前言
单元测试是一个程序员必备的技能,我在这里就不多说了,直接就写相应的代码吧。
单元测试基础类
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; //指定bean注入的配置文件
@ContextConfiguration(locations = { "classpath:/spring/spring_root.xml" ,"classpath:/spring/spring_mvc.xml"})
//使用标准的JUnit @RunWith注释来告诉JUnit使用Spring TestRunner
@RunWith(SpringJUnit4ClassRunner.class)
public class SpringTestCase extends AbstractJUnit4SpringContextTests { }
在这里配置好要加载的配置文件,然后用你写的普通单元测试类继承这个类,然后在相应的方法上加上@Test注解就可以进行单元测试了。
功能复杂的单元测试
例如DAO层,整合好的结构如下
首先,在src/test/java中写我们的测试类XXXXTest.java然后在类上面加上注解:
import java.util.Date;
import java.util.List; import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional; import com.macow.home.first.entity.User;
import com.macow.home.first.mapper.UserMapper; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/spring-context.xml")
@ActiveProfiles(value="dev")
@Transactional
public class UserMapperTest {
private Logger logger=LoggerFactory.getLogger(this.getClass());
@Autowired
private UserMapper userMapper; @Test
public void testUserInsert() {
User user=new User();
user.setName("杨过");
user.setPassword("222222");
user.setCreateDate(new Date());
userMapper.insert(user);
logger.info("--------->testUserInsert end-------------");
} @Test
public void testUserSelect() {
List<User> select = userMapper.select(null);
for(User user:select){
logger.info("--------->"+user.getName()+"-------------");
}
logger.info("--------->testUserInsert end-------------");
}
}
@Transactional这个非常关键,如果不加入这个注解配置,事务控制就会完全失效!
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)这里的事务关联到配置文件中的事务控制器(transactionManager = "transactionManager"),同时指定自动回滚(defaultRollback = true)。这样做操作的数据才不会污染数据库!
AbstractTransactionalDataSourceSpringContextTests要想构建这一系列的无污染纯绿色事务测试框架就必须找到这个基类!(即所有事务均不生效)
其次,在src/test/resource目录,我们只要放一个spring-context.xm配置文件,把所有的在src/main/resource下配置文件和资源文件加载进来就可以spring-context.xm里面加载到spring中就可以,所以这个目录一般就一个xml文件,其他都是配置的properties文件。
<?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: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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- <context:property-placeholder location="classpath*:jdbc.properties" /> --> <import resource="spring-dao.xml" />
<beans profile="dev" >
<context:property-placeholder location="classpath*:jdbc-dev.properties" />
</beans>
<beans profile="sit" >
<context:property-placeholder location="classpath*:jdbc-sit.properties" />
</beans>
</beans>
需要注意的地方:
测试方法命名:不能叫test方法,类也不能叫Test类
使用Spring+Junit4进行测试的更多相关文章
- 使用Spring+Junit4.4进行测试(使用注解)
http://nottiansyf.iteye.com/blog/345819 使用Junit4.4测试 在类上的配置Annotation @RunWith(SpringJUnit4ClassRunn ...
- 用Spring+Junit4.4进行测试(使用注解)
http://nottiansyf.iteye.com/blog/345819 使用Junit4.4测试 在类上的配置Annotation @RunWith(SpringJUnit4ClassRunn ...
- 使用Spring+Junit4.4进行测试
http://nottiansyf.iteye.com/blog/345819 使用Junit4.4测试 在类上的配置Annotation @RunWith(SpringJUnit4ClassRunn ...
- activemq spring 集成与测试
1.下载安装activemq 2.pom依赖配置 3.spring配置 4.生产消息,消费消息(同步消费),监听消息(异步消费) 4.测试 5.参考博客 http://www.cnblogs.com/ ...
- Spring MVC的测试
测试是保证软件质量的关键. 与 Spring MVC 相关的测试,主要涉及控制器的测试. 为了测试Web项目通常不需要启动项目,需要一些Servlet相关的一些模拟对象,比如MockMVC.MockH ...
- Spring Junit4 接口测试
Junit实现接口类测试 - dfine.sqa - 博客园http://www.cnblogs.com/Automation_software/archive/2011/01/24/1943054. ...
- Spring Boot从入门到放弃-Spring Boot 整合测试
站长资讯摘要:使用Spring Boot 整合测试,对Controller 中某个方法进行测试或者对Service,Mapper等进行测试,不需要运行项目即可查看运行结果是否和期望值相同,Spring ...
- Struts2+Spring+Mybatis+Junit 测试
Struts2+Spring+Mybatis+Junit 测试 博客分类: HtmlUnit Junit Spring 测试 Mybatis package com.action.kioskmoni ...
- spring Scurity终于测试OK了,复杂的功能还待深入研究!发布出来一起探讨吧!
spring Scurity终于测试OK了,复杂的功能还待深入研究!发布出来一起探讨吧! 就是因 为研究它,我的个天啦!头都大了一圈!还待修改完整版!我的目标不是每个项目拿到它就能使用!到时再说啦.. ...
随机推荐
- 通过优化在UE4中实现良好性能和高质量视觉效果
转自:http://gad.qq.com/program/translateview/7160166 译者:赵菁菁(轩语轩缘) 审校:李笑达(DDBC4747) 对于任何追求UE4性能最佳.同时又想 ...
- python程序里加入调试断点
在需要打断的地方加入:import pdb;pdb.set_trace()即可 如:
- 【BZOJ】1257: [CQOI2007]余数之和(除法分块)
题目 传送门:QWQ 分析 大佬和我说本题是除法分块,莫比乌斯反演中也有用到. QwQ我不会莫比乌斯反演啊~ 题目让我们求 $ \sum_{i=1}^n k\mod n $ 然后根据$ a \mo ...
- Hive基础之Hive表常用操作
本案例使用的数据均来源于Oracle自带的emp和dept表 创建表 语法: CREATE [EXTERNAL] TABLE [IF NOT EXISTS] [db_name.]table_name ...
- MapReduce On YARN
MapReduce计算框架 将计算过程分为两个阶段:Map和Reduce Map阶段并行处理输入数据: Reduce阶段对Map结果进行汇总 Shuffle连接Map和Reduce两个阶段 Map T ...
- Python while 循环使用实例
while循环是在Python中的循环结构之一. while循环继续,直到表达式变为假.表达的是一个逻辑表达式,必须返回一个true或false值,本文章向码农介绍Python while 循环使用方 ...
- node和yarn
nvm 版本管理工具 https://github.com/coreybutler/nvm-windows/releases nvm-setup nvm install +版本号 加版本 ...
- hdu 4370 0 or 1,最短路
题目描述 给定n * n矩阵C ij(1 <= i,j <= n),我们要找到0或1的n * n矩阵X ij(1 <= i,j <= n). 此外,X ij满足以下条件: 1. ...
- 反射(hasattr , getattr, setattr) 输入的字符串用来运行程序
当用户输入字符串时,不能够用来运行程序 1.使用 hasattr 找出输入的字符串是否在程序内 2.使用 getattr 返回找出字符串对应的函数的内存地址或者变量 3. 使用setattr 添加新的 ...
- 18 网络编程-TCP/IP各层介绍(5层模型讲解)
1.TCP/IP五层协议讲解 物理层--数据链路层--网络层--传输层--应用层 我们将应用层,表示层,会话层并作应用层,从tcp/ip五层协议的角度来阐述每层的由来与功能,搞清楚了每层的主要协议 就 ...