您的位置:首页 > 其它

maven配置JUnit环境

2017-10-09 19:28 246 查看
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>
注意tomcat-juli必要引入,否则会报错,错误信息:java.lang.ClassNotFoundException: org.apache.juli.logging.LogFactory

基础类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,

测试时,发现创建数据库链接快很多,并没有出现长时间等待现象。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: