web.xml中的Spring-test和ServletContextListener

Ale*_*kov 4 integration web.xml spring-test

我尝试通过此链接使用spring-test(3.2.10)和与TestNG的集成测试.

我创建了RootTest.java

@WebAppConfiguration
@ContextConfiguration("file:src/test/resources/root-context2.xml")
public class ReferenceServiceTest extends AbstractTestNGSpringContextTests {
...
Run Code Online (Sandbox Code Playgroud)

春天上下文加载成功.但我的全局变量没有实例化,因为web.xml被忽略了.在web.xml中,我有自己的"监听器类"(ServletContextListener的实现)和"context-param".我如何使用spring集成测试上下文加载web.xml上下文(并调用所有应用程序启动侦听器)?

Sam*_*nen 5

如参考手册中所述,Spring MVC测试框架......

"通过TestContext框架加载实际的Spring配置,并始终使用DispatcherServlet处理请求,从而近似完整集成测试,而无需运行的Servlet容器."

关键点是"没有......一个Servlet容器".因此web.xml这里没有进入图片.换句话说,配置web.xml无法使用Spring MVC测试框架对集成测试产生影响.

现在,有说,有可能注册一个Servlet FilterMockMvc这样的:

mockMvcBuilder.addFilters(myServletFilter);
Run Code Online (Sandbox Code Playgroud)

要么

mockMvcBuilder.addFilters(myResourceFilter, "/resources/*");
Run Code Online (Sandbox Code Playgroud)

您可以在执行断言之前context-param手动将它们添加到ServletContext(实际上是Spring MockServletContext)中来配置条目,MockMvc如下所示:

wac.getServletContext().setInitParameter(name, value);
Run Code Online (Sandbox Code Playgroud)

但是......没有办法配置ServletContextListener使用Spring MVC测试.如果您希望将侦听器应用于通过Spring MVC的所有请求,则可以考虑实现自定义HandlerInterceptorWebRequestInterceptor(请参阅参考手册中的配置拦截器).

问候,

山姆