0

I am trying to attach a parameter to a session using MockHttpServletRequest, but only declare a attribute in my test I am getting the followin error:

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.

I've tried do add <listener> <listener-class> org.springframework.web.context.request.RequestContextListener </listener-class> </listener>, like saying to do in this link: Getting a 'No thread-bound request found' error from spring in my web app

Test code:

@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"));
}

}

Can anyone help me, please?

Community
  • 1
  • 1
Danilo M.
  • 1,422
  • 5
  • 17
  • 31

1 Answers1

1

Configuring a listener in web.xml will have no effect on integration tests with the Spring MVC Test framework, since those tests run outside the Servlet container.

I obviously haven't seen the controller code you're trying to test, but if you can upgrade to Spring 3.2, I believe this should work out of the box thanks to the ServletTestExecutionListener that is registered by default. This works in conjunction with support for the new @WebAppConfiguration annotation introduced in Spring 3.2. See the examples in the reference manual for details.

If that doesn't work for you, please consider creating a test case that reproduces the error and/or opening a JIRA issue describing your problem in greater detail.

Cheers,

Sam

Sam Brannen
  • 29,611
  • 5
  • 104
  • 136
  • I am using Spring 3.1, at the moment the project that I work can't updrage to Spring 3.2, is there any way to do this using 3.1 version? Thanks. – Danilo M. Jun 26 '13 at 18:00