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,
测试时,发现创建数据库链接快很多,并没有出现长时间等待现象。