此处给出了我的问题的上下文:How to configure pom for StrutsTestCase so that web.xml is found?
在我的 pom.xml
中,我包含了以下内容,希望在测试期间提供各种文件:
<testResources>
<testResource>
<directory>src/test/java</directory>
<includes>
<include>*.*</include>
</includes>
</testResource>
<testResource>
<directory>webapp/WEB-INF</directory>
<includes>
<include>*.xml</include>
</includes>
</testResource>
</testResources>
我的期望是,src/test/java
中的所有内容都将在测试期间可用,web.xml
、struts-config.xml
在 webapp/WEB-INF
中。
测试期间未找到 web.xml
文件。在解决此问题时,我遇到两个问题。
- 我不完全确定我在哪里(或应该在哪里)告诉我的其他代码寻找
web.xml
- 我不知道如何在运行测试时验证
web.xml
是否可用。
现在,我要问#2。我如何验证上面的 XML 是否按照我的想法进行操作,并使 web.xml
可用于测试?
最佳答案
我认为您的配置问题可能是您已将“WEB-INF
”作为 <testResource>
的一部分包含在内。 。您可能想要的是更像这样的东西:
<testResources>
<testResource>
<directory>webapp</directory>
</testResource>
</testResources>
(或者,如果您使用 Standard Directory Layout 如前所述 here ,则 directory
将改为 src/main/webapp
。)
而且,如果您只想在 WEB-INF 目录中包含 XML 文件,那么您将需要一些额外的“includes
”配置:
<testResources>
<testResource>
<directory>webapp</directory>
</testResource>
<includes>
<include>WEB-INF/*.xml</include>
</includes>
</testResources>
为了测试在测试运行期间这些文件在类路径上实际上可用,您可以执行如下操作:
URL webXml = getClass().getClassLoader().getResource("WEB-INF/web.xml");
assumeNotNull(webXml);
(如前所述 here , assumeNotNull
的使用是可选的。)
关于java - 如何确定哪些资源可用于使用 Maven 进行测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7080748/