Spring整合Junit4进行单元测试
一. 添加依赖包(maven)
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.3.12.RELEASE</version> </dependency>
二. 创建测试基类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring/applicationContext.xml"})
public class BaseJunit4Test {
}
三. 创建测试类
public class RedisTest extends BaseJunit4Test{
@Resource
private SpringRedis springRedis;
@Test
public void testRedisSentinel(){
String key = "test";
springRedis.set(key, "111");
String value = springRedis.get(key);
System.out.println(value);
}
}