본문 바로가기

정리/junit

junit 기본 사용

참고사이트 모음 
[Mockito]Spring framework에서 Controller 테스트케이스 만들기 : 
http://bluepoet.me/2012/03/07/mockitospring-framework%EC%97%90%EC%84%9C-controller-%ED%85%8C%EC%8A%A4%ED%8A%B8%EC%BC%80%EC%9D%B4%EC%8A%A4-%EB%A7%8C%EB%93%A4%EA%B8%B0/ 
JUnit을 이용한 단위 테스트 : http://using.tistory.com/54 
Spring-Test-MVC프로젝트는 무엇일까 : http://helloworld.naver.com/helloworld/textyle/1341 
Spring Framework 3.2 RC1: Spring MVC Test Framework : http://spring.io/blog/2012/11/12/spring-framework-3-2-rc1-spring-mvc-test-framework 
JUnit Test Case : http://www.egovframe.go.kr/wiki/doku.php?id=egovframework:dev2:tst:test_case 
Spring-Test-MVC 프로젝트 소개 : http://helloworld.naver.com/helloworld/textyle/1341 
mockito : https://code.google.com/p/mockito/wiki/MockitoFeaturesInKorean

정리 
@RunWith() 
스프링의 테스트 컨텍스트 프레임워크 JUnit 확장 기능 지정 
Junit은 각각의 테스트가 서로 영향을 주지 않고 독립적으로 실행됨을 원칙으로 하기에 @Test 마다 오브젝트를 생성한다. 
이와 같은 Junit의 특성으로 인하여 ApplicationContext도 매번 새로 생성되기 때문에 테스트가 느려지는 단점이 있다. 
그러나 @RunWith 애노테이션은 각각의 테스트 별로 오브젝트가 생성 되더라도 싱글톤의 ApplicationContext를 보장한다. 
@RunWith() 대신 AbstractJUnit4SpringContextTests를 상속받아 사용할 수 있음.

@ContextConfiguration() 
spring bean 메타 설정 파일의 위치를 지정할 때 사용되는 애노테이션이며 경로를 지정하지 않으면 테스트 클래스 파일이 있는 패키지 내에서 다음의 설정 파일을 사용한다. 
ContextConfigLocationTest-context.xml 
contextconfiglocationtest-context.xml 
대소문자 구분 없음 
출처: http://lng1982.tistory.com/94

  • 샘플 소스
package com.sds.acube.luxor.common.service.impl;

import static org.junit.Assert.*; // static 으로 import 한다.

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.sds.acube.luxor.common.service.AccessControllService;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"file:webapp/WEB-INF/classes/spring/context/context-*.xml",
    "file:webapp/WEB-INF/classes/spring/servlet/common-*.xml"})
public class TestAccessControllServiceImpl {

  @Inject
  @Named("accessControllService")
  private AccessControllService accessControllService;

  private Logger logger = LoggerFactory.getLogger("ep.common");

  String resourceId;
  List<String> accessIdList;

  @Before
  public void setup() {
    resourceId = "N201501271115144201065";
    accessIdList = new ArrayList<String>();
    accessIdList.add("COMMON");
  }

  @Test
  public void canAccess() throws Exception {
    boolean result = accessControllService.canAccess(resourceId, accessIdList);
    logger.debug("{}", result);

    assertEquals(result, true);
  }

  @Test
  public void getAccess() throws Exception {
    String result = accessControllService.getAccess(resourceId);
    logger.debug("{}", result);

    assertNotNull(result);
  }

}
  • pom.xml 설정 

    org.springframework 
    spring-test 
    ${spring.maven.artifact.version} 


org.mockito 
mockito-all 
1.10.8 
test 

  • import 시 주의사항. (static 으로 해줘야 하는 것들이 있다.)

import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*; 
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; 
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; 
import static org.junit.Assert.*; 
import static org.mockito.Matchers.*; 
import static org.mockito.Mockito.*;

Written with StackEdit.