使用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了,复杂的功能还待深入研究!发布出来一起探讨吧! 就是因 为研究它,我的个天啦!头都大了一圈!还待修改完整版!我的目标不是每个项目拿到它就能使用!到时再说啦.. ...
随机推荐
- Goroutine(协程)为何能处理大并发?
简单来说:协程十分轻量,可以在一个进程中执行有数以十万计的协程,依旧保持高性能. 进程.线程.协程的关系和区别: 进程拥有自己独立的堆和栈,既不共享堆,亦不共享栈,进程由操作系统调度. 线程拥有自己独 ...
- 生产者消费者模型(Queue,JoinableQueue)
生产者消费者模型 主要是为解耦 借助队列来实现生产者消费者模型 栈:先进后出(First In Last Out 简称 FILO) 队列: 先进先出(First In First Out 简称 FIF ...
- Bogart gSub.vb
'--------------Job No 0900408 -------------- '--DIM PART ONE ONLINE Update Order Qty '''主要新加過程名 Refr ...
- 2018-2019-2 《网络对抗技术》Exp1 PC平台逆向破解 Week3 20165233
Exp1 PC平台逆向破解 实验内容 一.基础知识点 NOP, JNE, JE, JMP, CMP汇编指令的机器码 NOP指令即"空指令",执行到NOP指令时,CPU什么也不做,机 ...
- 0_Simple__simpleTexture + 0_Simple__simpleTextureDrv
使用纹理引用来旋转图片,并在使用了静态编译和运行时编译两种环境. ▶ 源代码:静态编译 #include <stdio.h> #include <windows.h> #inc ...
- vmware桥接模式无法上网
环境:本机win10系统,ip地址固定,(估计存在vlan网络), 状况:vmware的nat模式可以上网,桥接模式不能上网, 解决办法:找网管把本机设置成dhcp模式,才行了
- 34. Studio字符串分割split用法
var v = "1,2,3"; var arr = v.toString().split(","); 备注:最好先toString()转为字符串,不然有些情况 ...
- cnapckSurround c++builder Region 代码折叠快捷键
C++Builder代码折叠 cnapckSurround c++builder Region 代码折叠快捷键,可以导入导出,IDE code edit,cnpack menu surround wi ...
- Spring boot @ConfigurationProperties 和@Value
@ConfigurationProperties @Value 功能 批量注入配置文件中的属性 一个个指定 松散绑定(松散语法) 支持 不支持 SpEL 不支持 支持 JSR303数据校验 支持 ...
- android -chrome 调试
在chrome上 输入 chrome://inspect/ 连接手机,配置 监听8000,和8080端口 cordova默认是8000端口 如果出现白屏:原因:google在首次加载时,要进行服务器连 ...