0

我正在尝试使用 将参数附加到会话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.

我试过 do add <listener> <listener-class> org.springframework.web.context.request.RequestContextListener </listener-class> </listener>,就像在这个链接中说的那样:在我的网络应用程序中从 spring 中获取 'No thread-bound request found' 错误

测试代码:

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

}

任何人都可以帮助我吗?

4

1 回答 1

1

配置监听web.xml器不会影响使用 Spring MVC 测试框架的集成测试,因为这些测试在 Servlet 容器之外运行。

我显然没有看到您要测试的控制器代码,但是如果您可以升级到 Spring 3.2,我相信这应该可以开箱即用,这要归功于ServletTestExecutionListener默认注册的。@WebAppConfiguration这与对Spring 3.2 中引入的新注释的支持结合使用。有关详细信息,请参阅参考手册中的示例。

如果这对您不起作用,请考虑创建一个重现错误的测试用例和/或打开更详细地描述您的问题的 JIRA 问题。

干杯,

山姆

于 2013-06-26T16:53:23.050 回答