我正在使用 Maven 2 构建一个 Java - Sturts 1.2 - Spring 1.2 项目。我正在尝试合并 JUnit 的 StrutsTestCase 扩展以允许测试操作类。
当我尝试构建时,出现以下错误:
junit.framework.AssertionFailedError: The /WEB-INF/web.xml was not found.
at servletunit.struts.MockStrutsTestCase.getActionServlet(MockStrutsTestCase.java:344)
at servletunit.struts.MockStrutsTestCase.tearDown(MockStrutsTestCase.java:130)
在研究这个过程中,我学到了以下内容:
- 如果您的
web.xml
文件位于非标准位置,则可以使用setServletConfigFile()
向StrutsTestCase
指示位置文件是。 - 在使用 Maven 构建时,可能需要指明
pom.xml
中应包含哪些资源。
我正在努力实现这个,这是我目前的进展:
测试类:
import servletunit.struts.MockStrutsTestCase;
public class FooActionTest extends MockStrutsTestCase {
public void setUp() throws Exception {
super.setUp();
setServletConfigFile("webapp/WEB-INF/web.xml");
}
public void testTest() throws Exception { /* stub, no content */ }
我在 pom.xml
的 build
标签中包含了以下 testResources
。
<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>
这是项目的结构:
myproject
│---src
│ |---main
| | |---java
| |
| |---test
| |---Java
|
|---target
| |---classes
| |
| |---MYPROJECT
| |---META-INF
| |---WEB-INF
| |---struts.config.xml
| |---web.xml
|
|---webapp
| |---META-INF
| |---WEB-INF
| |---struts.config.xml
| |---web.xml
|---pom.xml
当然,我已经在pom.xml
和setServletConfigFile
中尝试了各种路径变体。我也尝试过使用 setContextDirectory
,但没有不同的结果。
最佳答案
我正在使用默认的 Maven webapp 结构
myproject
|---src
| |---main
| | |---java
| |
| |---test
| | |---Java
| |
| |---webapp
| |---WEB-INF
| |---struts.config.xml
| |---web.xml
|
|---pom.xml
我正在将 webapp XML 文件添加到我的测试资源中,我不需要调用 setServletConfigFile 方法。
<project>
...
<build>
...
<testResources>
<testResource>
<directory>src/main/webapp/WEB-INF</directory>
<targetPath>/WEB-INF</targetPath>
<includes>
<include>*.xml</include>
</includes>
</testResource>
</testResources>
..
</build>
...
</project>
因此,在您的情况下,您应该在 POM 文件中添加这些行:
<project>
...
<build>
...
<testResources>
<testResource>
<directory>webapp/WEB-INF</directory>
<targetPath>/WEB-INF</targetPath>
<includes>
<include>*.xml</include>
</includes>
</testResource>
</testResources>
..
</build>
...
</project>
关于java - 如何为 StrutsTestCase 配置 pom 以便找到 web.xml?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7070611/