2013-06-25 193 views
0

我試圖附加參數來使用MockHttpServletRequest會話,但只申報在我的測試我正在跟隨着錯誤的屬性配置MockHttpServletRequest:在單元控制器測試

Caused by: java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request. 

我試着做補充<listener> <listener-class> org.springframework.web.context.request.RequestContextListener </listener-class> </listener>,好像是說在這個環節上做的:Getting a 'No thread-bound request found' error from spring in my web app

測試代碼:

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(loader = WebContextLoader.class, classes = { TestApplicationWebContext.class }) 
public class ClienteControllerTest { 

private MockMvc mockMvc; 

@Resource 
private WebApplicationContext webApplicationContext; 

@Autowired 
private org.springframework.mock.web.MockHttpServletRequest request; 

@Before 
public void inicializacao() { 
    mockMvc = MockMvcBuilders.webApplicationContextSetup(webApplicationContext).build(); 
} 

@Test 
public void teste() throws Exception { 
    mockMvc.perform(get("/administracao/cliente")).andExpect(status().isOk()).andExpect(forwardedUrl("administracao-cliente/index")); 
} 

}

任何人都可以幫助我嗎?

回答

1

web.xml中配置偵聽器將不會影響Spring MVC測試框架的集成測試,因爲這些測試在Servlet容器之外運行

我明顯沒有看到你想要測試的控制器代碼,但是如果你可以升級到Spring 3.2,我相信這應該是開箱即用的,這要歸功於默認註冊的ServletTestExecutionListener。這與支持Spring 3.2中引入的新@WebAppConfiguration註解一起工作。有關詳細信息,請參閱reference manual中的示例。

如果這不適用於您,請考慮creating a test case that reproduces the error和/或opening a JIRA issue更詳細地描述您的問題。

乾杯,

山姆

+0

我使用Spring 3.1,此刻,我的工作不能updrage到Spring 3.2的項目,有沒有辦法做到這一點使用3.1版本?謝謝。 –