2013-05-21 32 views
0

我想在我的spring mvc控制器中進行測試,並且我的applicationContext始終爲null。@WebApplicationContext中的自動化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

您尚未提供任何上下文配置位置。所以,spring不會定位文件,從中可以創建bean並在需要時注入。

請記住,主應用程序和測試的上下文配置位置是不同的。所以,即使你已經在web.xml中指定它,它的作用域不包含測試。你將不得不在你的測試類中明確地指定它。