2011-09-06 289 views
4

我試圖在測試用例中使用web.xml。但是,我不能......Junit測試用例,如何讀取web.xml

@ContextConfiguration(locations={"file:src/main/webapp/WEB-INF/web.xml", 
    "/integration/restful/*.xml"}  
public class RestfulTest { } 

錯誤消息:

Caused by: org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://java.sun.com/xml/ns/javaee] Offending resource: URL [file:src/main/webapp/WEB-INF/web.xml] 

web.xml文件的標題:

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

我能做些什麼?

回答

3

@ContextConfiguration正在尋找Spring應用程序上下文配置文件。 web.xml不是Spring配置文件,而是一個在servlet容器中配置Web應用程序的文件。

Spring應用程序上下文文件描述了你的Spring管理的bean和依賴關係。請參閱:http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/testing.html

由於Spring試圖根據Spring應用程序上下文文件的模式解析web.xml文件,因此測試失敗。