2014-12-06 3 views
2

사용자 정의 프레임 워크 종속성으로 인해 Spring 3.2 이상으로 업그레이드 할 수 없습니다. 나는 3.1.x에 붙어있다. 그래서 같은 봄 JUnit 테스트에서의 WebApplicationContext를 얻기 위해 노력하고 있어요 :Spring 3.1에서 WebApplicationContext 가져 오기 Junit 테스트

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = "file:src/main/webapp/WEB-INF/spring/context.xml") 
public class WebIntegrationTest { 

    @Autowired 
    private WebApplicationContext ctx; 

이 예상대로 WebApplicationContext를로드하지 않습니다.

스프링 3.2 이상에서 스프링에 내가 제공 한 웹 컨텍스트를 원한다는 것을 알려주기 위해 @WebAppConfiguration을 사용할 수는 있지만, 어떻게하면 내 의존성 제약 조건으로이를 수행 할 수 있는지 알 수 있습니까?

그것은 너무하지 ...하지만이 작업의 WebApplicationContext를 얻을 수있을 때까지 충분히 테스트 할 수 없습니다 -

+1

수 없습니다. 적어도 일반적인 스프링 클래스를 사용하지 않는 경우, 직접 작성하고'XmlWebApplicationContext'를 직접 만들고 MockServletContext를 사용하여 웹 의존성을 조롱해야합니다. imho를 강제로 업그레이드하는 것이 더 쉽습니다. –

답변

2

나도 내가 업그레이드하려면이 코드 주위에 충분한 테스트를 얻을 수 있습니다 전까지는, 3.1에 붙어 있어요 WebApplicationContext를 구축하기가 어려우며 시행 착오를 하루 아침에 마친다.

그래서,

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(loader = MyLoader.class, 
    locations = 
    { 
    "file:WEB-INF/springcontexts/spring-security-common.xml", 
    "file:WEB-INF/springcontexts/webapp-common.xml" 
}) 
public class LoginIntegrationTest { 

같은 시험을 위해 당신은 당신이 당신의 자신을 일치하도록 경로를 변경해야하거나, 주변에 어떻게 든 전달할 수 있습니다

public class MyLoader implements ContextLoader { 

    @Override 
    public String[] processLocations(Class<?> clazz, String... locations) { 
     return locations; 
    } 

    @Override 
    public ApplicationContext loadContext(String... locations) throws Exception { 
     XmlWebApplicationContext result = new XmlWebApplicationContext(); 
     MockServletContext servletContext = new MockServletContext("target/webapp", new FileSystemResourceLoader()); 
     result.setServletContext(servletContext); 
     result.setServletConfig(new MockServletConfig(servletContext)); 
     result.setConfigLocations(locations); 
     result.refresh(); 
     return result; 
    } 
} 

을 정의 할 수 있습니다.

관련 문제