Language/Java

Spring Test case sample

아르비스 2016. 5. 20. 10:14

test case에서 Spring Context Configuration을 로딩하기가 쉽지 않다.


다음과 같이 하면.. Context를 쉽게 로딩할수 있다.

@Autowired를 이용하거나, @Resouce를 통해서 직접 지정한다.


@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations = {

        "file:src/main/webapp/WEB-INF/spring/root-context.xml"

})

public class RedisTemplateTest {

@Autowired

RedisTemplate redisTempate;

@Test

public void redisTest() {

redisTempate.opsForValue().set("testKey", "testValue");

}

}



혹은


@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations = {

        "file:src/main/webapp/WEB-INF/spring/root-context.xml"

})

public class RedisConnectorTest{

@Resource(name="com.aaa.connector.redis.RedisConnector")

RedisConnector connector;

@Test

public void expireTest() {

Map<Object, Object> dataMap = new HashMap<Object, Object>();

String key = "sncapRedisKey";

        dataMap.put("TOKEN", "ABCDEFGHIJKLMN");

        dataMap.put("TICKET", "OPQRSTUVWXYZ");

        dataMap.put("userEmail", "sncap@samsung.com");

        

        connector.putAllValue(key, dataMap);

        connector.setKeyExpire(key, 100);


        boolean retryCheck = true;

        

        System.out.println("Key : " + key );

        

}