JUnit test에서 application context를 로딩하기
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={
"file:src/main/webapp/WEB-INF/spring/root-context.xml",
"file:src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml"})
이렇게 해주면 로딩이 잘 됩니다. maven을 통한 test도 잘 진행되구요. 그런데 한가지 우려되는 것은 모든 테스트 클래스에 이 걸 다 넣으려면 짜증나겠죠? 까먹을 수도 있고 중간에 bean configuration 파일의 위치나 이름이 변경되면 모든 테스트 클래스를 열어서 다 바꿔줘야 합니다. 이럴 때는 상속을 사용하면 편리합니다.
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={ "file:src/main/webapp/WEB-INF/spring/root-context.xml", "file:src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml"}) public abstract class AbstractApplicationContextTest { @Autowired protected ApplicationContext context; }
위와 같이 클래스를 만들고 Application Context가 필요한 테스트 클래스에서 상속해서 사용하면 됩니다.
퍼온곳 == http://hightin.tistory.com/42