8
我已經多次閱讀文檔(http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/testing.html#spring-mvc-test-framework),並且我無法確認在使用@WebApplicationContext
註釋時注入的WebApplicationContext
上下文是否實際查看web.xml。Spring 3.2中發佈的新Spring MVC測試框架是否測試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>
謝謝。這很有幫助。如果不讀取web.xml,我仍不確定爲什麼創建mock需要Web應用程序根目錄的位置。看到我對拉爾夫的回答http://stackoverflow.com/a/14026570/411229的評論。 –
,因爲它確實使用了來自Web應用程序根的其他資源 - 例如。如果您已將Spring Web應用程序配置置於WEB-INF/spring/appcontext.xml中,儘管不在類路徑中,它仍然能夠訪問並將其加載到測試中。 –
這是有道理的。在這一點上文件不是很清楚。感謝您清理它。 –