spring配置JUnit环境

pom.xml

<!-- JUNIT测试架包 -->
<dependency>
 	<groupId>junit</groupId>
	<artifactId>junit</artifactId>
	<version>4.12</version>
	<scope>test</scope>
</dependency>
<dependency>
	<groupId>org.apache.tomcat</groupId>
	<artifactId>tomcat-juli</artifactId>
	<version>7.0.21</version>
	<scope>test</scope>
</dependency>
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-test</artifactId>
	<version>${srping.version}</version>
	<scope>test</scope>
</dependency>

如果报:java.lang.ClassNotFoundException: org.apache.juli.logging.LogFactory。
可以尝试引入tomcat-juli这个包

基础类BaseTest.java

import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * 测试基类
 *
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/spring/applicationContext-service.xml",
			"classpath:/spring/applicationContext-dao.xml",
			"classpath:/spring/applicationContext-redis.xml"})
public class BaseTest {
	// 
	//  extends AbstractTransactionalJUnit4SpringContextTests 需要回滚时继承
}

测试类

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import com.service.redis.RedisService;

import test.service.BaseTest;

public class RedisServiceTest extends BaseTest {
	@Autowired
	private RedisService redisService;
	
	@Test
	public void testGet() {
		System.out.println(redisService.get("address"));
	}
	
	@Test
	public void test() {
		System.out.println("xxx");
	}
}


项目结构目录参考之前一篇:http://blog.csdn.net/h996666/article/details/78124232

 

遗留问题:不知道为什么测试时创建数据库链接很慢,要卡很长时间。

--------------------------------------2017/10/18补充------------------------------------------------------------

测试时,创建数据库链接慢。还没搞清楚,我现在换了数据库的连接池,使用阿里的druid,

测试时,发现创建数据库链接快很多,并没有出现长时间等待现象。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值