【问题标题】:Does the new Spring MVC Test Framework released in Spring 3.2 test the web.xml configuration?Spring 3.2 发布的新 Spring MVC 测试框架是否测试 web.xml 配置?
【发布时间】:2012-12-11 03:46:14
【问题描述】:

我已多次阅读文档 (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。 (例如,我在 /myServletPath/foo 这样的 URL 上尝试了 get 请求,但它以 404 失败。)

我的测试:

@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 spring-mvc integration-testing web.xml spring-test-mvc


    【解决方案1】:

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

    webAppContextSetup(this.wac).addFilter(new DefaultWebappMetricsFilter(), "/*").build()
    

    【讨论】:

    • 谢谢。这很有帮助。如果它不读取 web.xml,我仍然不确定为什么模拟的创建需要 Web 应用程序根目录的位置。请参阅我对 Ralph 的回答 stackoverflow.com/a/14026570/411229 的评论。
    • 因为它确实使用了来自 Web 应用程序根目录的其他资源 - 例如。如果您已将 Spring Web 应用程序配置放在 WEB-INF/spring/appcontext.xml 中,尽管不在类路径中,它仍然可以在测试中访问和加载它。
    • 这是有道理的。在这一点上,文档不是很清楚。感谢您清理它。
    猜你喜欢
    • 1970-01-01
    • 2018-03-02
    • 2013-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-23
    • 2019-02-04
    相关资源
    最近更新 更多