2011-09-06 290 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文件,因此测试失败。