2013-05-21 5 views
0

스프링 MCQ 컨트롤러에서 테스트를하려고하는데 내 applicationContext는 항상 null입니다.WebApplicationContext에서 @Autowired Spring MVC 테스트가 작동하지 않습니다.

import org.springframework.beans.factory.annotation.Autowired 
import org.springframework.test.context.web.WebAppConfiguration 
import org.springframework.test.web.servlet.MockMvc 
import org.springframework.test.web.servlet.ResultActions 
import org.springframework.test.web.servlet.setup.MockMvcBuilders 
import org.springframework.web.context.WebApplicationContext 
import spock.unitils.UnitilsSupport 

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status 

@UnitilsSupport 
@WebAppConfiguration 
//@ContextConfiguration(classes=Config.class) 
//@ContextConfiguration(loader= ApplicationContextProvider.class) 
class TimeSheetControllerIt extends TestsSupport { 

    @Autowired 
    private WebApplicationContext webApplicationContext; //NULL 

    //private MockHttpServletRequest mockHttpServletRequest; 
    private MockMvc mockMvc 
    private ResultActions resultActions 

    def setup(){ 
     //this.mockHttpServletRequest = new MockHttpServletRequest(); 
     this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); 
    } 

어떻게해야합니까?

답변

0

컨텍스트 구성 (사용자가 주석 처리 한 @ContextConfiguration)을 제공하십시오.

응용 프로그램 컨텍스트가 제대로 초기화되었는지 로그를 확인하십시오.

0

컨텍스트 configlocation을 제공하지 않았습니다. 따라서 스프링은 파일의 위치가 아니기 때문에 빈을 생성하고 필요할 때마다 삽입 할 수 있습니다.

주요 응용 프로그램과 테스트의 컨텍스트 설정 위치가 다릅니다. 따라서 web.xml에서 지정한 경우에도 범위에 테스트가 포함되지 않습니다. 테스트 클래스에 명시 적으로 지정해야합니다.

관련 문제