bas*_*ode 5 java junit maven-2 web-applications
我编写了一个JUnit测试,用于验证服务针对其XSD返回的xml.我最初将XSD置于其中src/main/resrouces/xsd/<filename>.xsd但现在我面临着将其webapp作为静态资源移动的需要,以便在部署应用程序时可以公开访问它.
我宁愿没有两份副本,因为我希望有人迟早会修改错误的副本.
我正在加载文件getClass().getResource("/xsd/<filename>.xsd"),如果资源被移动,我将如何访问它webapp?
/src/main/webapp在创建和部署 WAR 时,内容被放置在 CLASSPATH 上,但在 JUnit 测试期间(在 maven 和我的 IDE 中)不会。您必须使用以下方法显式加载它们:
new File("src/main/webapp/xsd/<filename>.xsd");
Run Code Online (Sandbox Code Playgroud)
只要在运行测试时项目主目录是当前目录,这应该有效。悲伤但真实。
在您的 pom 中将其添加为资源目录。
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/webapp</directory>
<includes>
<include>xsd/schema.xsd</include>
</includes>
</resource>
</resources>
Run Code Online (Sandbox Code Playgroud)
在java中使用
String path = "/xsd/schema.xsd";
InputStream is = getClass().getResourceAsStream(path);
Run Code Online (Sandbox Code Playgroud)