1. 程式人生 > >Spring整合Junit4進行單元測試

Spring整合Junit4進行單元測試

CA spa dep pat unit ati 測試 ID sco

一. 添加依賴包(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);

  }
}

Spring整合Junit4進行單元測試