9

I've read the docs ( http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/testing.html#spring-mvc-test-framework ) several times and I can't confirm if the WebApplicationContext context that gets injected when you use the @WebApplicationContext annotation is actually looking at the web.xml.

In other words, I want to test my web.xml configuration. The filters and servlet path specifically. But when I configure my test it ignores the web.xml. (e.g. I try a get request on a URL like this /myServletPath/foo and it fails with a 404.)

My test:

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration({
        "classpath*:WEB-INF/config/application-context.xml",
        "classpath*:WEB-INF/oms-servlet.xml",
        "classpath*:persistence-context.xml"
})
public class OrderSummaryControllerIntegrationTests {

    @Autowired
    private WebApplicationContext wac;

    private MockMvc mockMvc;

    @Before
    public void setUp() throws Exception {
        this.mockMvc = webAppContextSetup(this.wac).build();
    }

    @Test
    public void testFindOrderSummariesExpectsSuccess() throws Exception {
        mockMvc.perform(get("/oms/orders?user=1234&catalog=bcs"))
                .andDo(print())
                .andExpect(status().isOk())
                .andExpect(content().contentType(MediaType.APPLICATION_JSON));
    }
}

And my web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
          http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">
    <display-name>OMS REST Services</display-name>

    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.css</url-pattern>
    </servlet-mapping>

    <filter>
        <filter-name>webappMetricsFilter</filter-name>
        <filter-class>com.yammer.metrics.web.DefaultWebappMetricsFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>webappMetricsFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/config/application-context.xml, classpath*:persistence-context.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>oms</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>oms</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

1 Answer 1

8

You are right, Spring-mvc-test does not read the web.xml file, but you can configure the filters this way:

webAppContextSetup(this.wac).addFilter(new DefaultWebappMetricsFilter(), "/*").build()
3
  • Thanks. That's helpful. I'm still not sure why the creation of the mock needs the location of the web application root if it's not reading the web.xml. See my comment on Ralph's answer stackoverflow.com/a/14026570/411229.
    – Ryan Walls
    Dec 25, 2012 at 0:30
  • 1
    because it does use other resources from the web application root - for eg. if you had placed your Spring web application configuration in say WEB-INF/spring/appcontext.xml, although not in the classpath it would still be able to access and load it in the test. Dec 25, 2012 at 0:31
  • that makes sense. The documentation isn't very clear on that point. Thanks for clearing it up.
    – Ryan Walls
    Dec 25, 2012 at 0:43

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.