Spring 3.2 中发布的新 Spring MVC 测试框架是否测试 web.xml 配置?

2023-11-22

我已阅读文档(http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/testing.html#spring-mvc-test-framework)好几次,我无法确认是否WebApplicationContext当您使用时注入的上下文@WebApplicationContext注释实际上是查看web.xml。

换句话说,我想测试我的 web.xml 配置。特别是过滤器和 servlet 路径。但是当我配置测试时它会忽略 web.xml。 (例如我尝试get请求这样的 URL/myServletPath/foo并且失败并返回 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));
    }
}

还有我的 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>

你是对的,Spring-mvc-test不会读取web.xml文件,但是你可以这样配置过滤器:

webAppContextSetup(this.wac).addFilter(new DefaultWebappMetricsFilter(), "/*").build()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring 3.2 中发布的新 Spring MVC 测试框架是否测试 web.xml 配置? 的相关文章

随机推荐