Spring -test and ServletContextListener in web.xml

I am trying to use spring -test (3.2.10) and integration tests with TestNG with this link.

I created RootTest.java

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

spring context loaded success. But my global variables are not created because web.xml is ignored. In web.xml, I have my own "listener class" (implementation of ServletContextListener) and "context-param". How can I load the web.xml context (and calls all listeners to launch applications) using the spring test testing context?

+4
source share
2 answers

As stated in the reference guide, Spring MVC Test Framework ...

" Spring TestContext DispatcherServlet , , ."

"... ". , web.xml . , web.xml , Spring MVC.

, , Servlet Filter MockMvc :

mockMvcBuilder.addFilters(myServletFilter);

mockMvcBuilder.addFilters(myResourceFilter, "/resources/*");

context-param, ServletContext ( Spring MockServletContext) MockMvc :

wac.getServletContext().setInitParameter(name, value);

... ServletContextListener Spring MVC Test. , , Spring MVC, HandlerInterceptor WebRequestInterceptor (. ).

,

+5

MockServletContext

@Before
public void before() {
    MockServletContext mockServletContext = new MockServletContext();
    mockServletContext.setInitParameter("parameterName", "parameterValue");
    new MyListenerClass().contextInitialized(new ServletContextEvent(mockServletContext));
}
0

All Articles