spring Test 整合Junit4 使用总结

spring Test 整合Junit4 使用总结:

现在大部分都是基于maven的工程,分工程分模块开发,每个工程都要可以单独测试,下面说下这两个框架的整合。

例如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")配置环境选择,这个事spring的另一个功能点,大家可以参考我的另一篇文章,

其次,在src/test/resource目录,我们只要放一个spring-context.xm配置文件,把所有的在src/main/resource下配置文件和资源文件加载进来就可以,如果资源文件不再这一层,我们可以复制一份到src/test/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类



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值