i try to use spring-test(3.2.10) and integration tests with TestNG by 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 not instantiated because web.xml ignored. In web.xml i have my own "listener-class"(implementation of ServletContextListener) and "context-param". How i can load web.xml context(and calls all application startup listeners) with spring integration test context?
As stated in the reference manual, the Spring MVC Test Framework...
"loads the actual Spring configuration through the TestContext
framework and always uses the DispatcherServlet to process requests
thus approximating full integration tests without requiring a running
Servlet container."
The key point there is "without ... a Servlet container". Thus web.xml
does not come into the picture here. In other words, there is no way for configuration in web.xml
to have an affect on integration tests using the Spring MVC Test Framework.
Now, having said that, it is possible to register a Servlet Filter
with MockMvc
like this:
mockMvcBuilder.addFilters(myServletFilter);
or
mockMvcBuilder.addFilters(myResourceFilter, "/resources/*");
And you can configure context-param
entries by adding them manually to the ServletContext
(which is actually Spring's MockServletContext
) before you execute assertions on MockMvc
like this:
wac.getServletContext().setInitParameter(name, value);
But... there is no way to configure a ServletContextListener
using Spring MVC Test. If you want to have a listener applied to all of your requests that pass through Spring MVC, as an alternative you could consider implementing a custom HandlerInterceptor
or WebRequestInterceptor
(see Configuring interceptors in the reference manual).
Regards,
Sam