前言

  单元测试是一个程序员必备的技能,我在这里就不多说了,直接就写相应的代码吧。

单元测试基础类

  

 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-------------");
}
}
@RunWith(SpringJUnit4ClassRunner.class)SpringJUnit支持,由此引入Spring-Test框架支持!
@ContextConfiguration(locations = "classpath:applicationContext.xml") 多个配置文件的话可以用数组表示{“applicationContext.xml”,“applicationContext1.xml”},下面我会贴我的配置文件,只有一个配置文件;
@ContextConfiguration("/spring-context.xml")放在根路径下(即类路径下),然后<import resource="spring-dao.xml" />所有的配置文件和资源文件
@Transactional这个非常关键,如果不加入这个注解配置,事务控制就会完全失效! 
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)这里的事务关联到配置文件中的事务控制器(transactionManager = "transactionManager"),同时指定自动回滚(defaultRollback = true)。这样做操作的数据才不会污染数据库! 
AbstractTransactionalDataSourceSpringContextTests要想构建这一系列的无污染纯绿色事务测试框架就必须找到这个基类!(即所有事务均不生效)
@ActiveProfiles(value="dev")配置环境选择

其次,在src/test/resource目录,我们只要放一个spring-context.xm配置文件,把所有的在src/main/resource下配置文件和资源文件加载进来就可以spring-context.xm里面加载到spring中就可以,所以这个目录一般就一个xml文件,其他都是配置的properties文件。

我的配置文件spring-context.xml:
 <?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进行测试的更多相关文章

  1. 使用Spring+Junit4.4进行测试(使用注解)

    http://nottiansyf.iteye.com/blog/345819 使用Junit4.4测试 在类上的配置Annotation @RunWith(SpringJUnit4ClassRunn ...

  2. 用Spring+Junit4.4进行测试(使用注解)

    http://nottiansyf.iteye.com/blog/345819 使用Junit4.4测试 在类上的配置Annotation @RunWith(SpringJUnit4ClassRunn ...

  3. 使用Spring+Junit4.4进行测试

    http://nottiansyf.iteye.com/blog/345819 使用Junit4.4测试 在类上的配置Annotation @RunWith(SpringJUnit4ClassRunn ...

  4. activemq spring 集成与测试

    1.下载安装activemq 2.pom依赖配置 3.spring配置 4.生产消息,消费消息(同步消费),监听消息(异步消费) 4.测试 5.参考博客 http://www.cnblogs.com/ ...

  5. Spring MVC的测试

    测试是保证软件质量的关键. 与 Spring MVC 相关的测试,主要涉及控制器的测试. 为了测试Web项目通常不需要启动项目,需要一些Servlet相关的一些模拟对象,比如MockMVC.MockH ...

  6. Spring Junit4 接口测试

    Junit实现接口类测试 - dfine.sqa - 博客园http://www.cnblogs.com/Automation_software/archive/2011/01/24/1943054. ...

  7. Spring Boot从入门到放弃-Spring Boot 整合测试

    站长资讯摘要:使用Spring Boot 整合测试,对Controller 中某个方法进行测试或者对Service,Mapper等进行测试,不需要运行项目即可查看运行结果是否和期望值相同,Spring ...

  8. Struts2+Spring+Mybatis+Junit 测试

    Struts2+Spring+Mybatis+Junit 测试 博客分类: HtmlUnit Junit Spring 测试 Mybatis  package com.action.kioskmoni ...

  9. spring Scurity终于测试OK了,复杂的功能还待深入研究!发布出来一起探讨吧!

    spring Scurity终于测试OK了,复杂的功能还待深入研究!发布出来一起探讨吧! 就是因 为研究它,我的个天啦!头都大了一圈!还待修改完整版!我的目标不是每个项目拿到它就能使用!到时再说啦.. ...

随机推荐

  1. 常用模块:hashlib,subprocess,configparser。

    一  hashlib模块 那么我们前面学习数据类型的时候,也讲了hash,可变类型不可hash:不可变类型可hash. 我们知道hash是一种算法,接收传入的内容经过运算之后得到一个hash值,我们可 ...

  2. AVL树Python实现

    # coding=utf-8 # AVL树Python实现 def get_height(node): return node.height if node else -1 def tree_mini ...

  3. Java反射 - 简单的给Bean赋值和取值

    由于项目的实际需要,所以利用java反射原理写了一个简单给bean赋值和取值通用的类,在此记录下方便自己日后用到,也为需要的兄弟提供个参考例子. 工具类BeanRefUtil:   package c ...

  4. 同步锁源码分析(一)AbstractQueuedSynchronizer原理

    文章转载自 AbstractQueuedSynchronizer的介绍和原理分析 建议去看一下原文的评论,会有不少收获. 简介 AbstractQueuedSynchronizer 提供了一个基于FI ...

  5. 并发工具类(四)线程间的交换数据 Exchanger

    前言   JDK中为了处理线程之间的同步问题,除了提供锁机制之外,还提供了几个非常有用的并发工具类:CountDownLatch.CyclicBarrier.Semphore.Exchanger.Ph ...

  6. 表单:提交验证,及blur事件验证

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  7. Appium清空EditText

    在使用appium过程中,发现sendkeys和clear方法并不太好使,封装模拟手工一个一个删除 这里用到keyEvent,具体内容请参考api http://appium.github.io/ja ...

  8. jpa 多对多

    entity   Item package entity; import java.util.HashSet; import java.util.Set; import javax.persisten ...

  9. Redis 几个全局命令, 以及事物

    1, 清空当前数据库的所有数据 =>  flushdb 2, 清空所有数据库的所有数据 => flushall 3, key 值检索命令 => scan num match if 会 ...

  10. Redis 在 LINUX 系统下 安装, 启动

    01, 下载  http://www.redis.cn/ ,  这里下再下来的是 redis-4.0.1.tar.gz 这个压缩包 02, 将压缩包放到 linux 系统中,  一般放在 usr/lo ...