java - 如何为 StrutsTestCase 配置 pom 以便找到 web.xml?

标签 java unit-testing maven junit struts-1

我正在使用 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)

在研究这个过程中,我学到了以下内容:

  1. 如果您的 web.xml 文件位于非标准位置,则可以使用 setServletConfigFile()StrutsTestCase 指示位置文件是。
  2. 在使用 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.xmlbuild 标签中包含了以下 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.xmlsetServletConfigFile 中尝试了各种路径变体。我也尝试过使用 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/

相关文章:

java - 获取括号之间的字符串,排除其他括号

unit-testing - 戈朗 : Replace function unit testing

java - Maven 构建后缺少类文件

java - 找不到 com.fasterxml.jackson.core.JsonParseException 的类文件

java - 计算学生的 GPA

java - 单例设计和其中的新对象

java - Eclipse 内部错误 "Polling news feeds"

python - 为什么添加 Tee 会改变输出顺序

c# - 是否可以模拟 .NET HttpWebResponse?

maven - lib/javax.inject-1.jar 中的 javax/inject/Inject.class 被 onejar 可执行文件上的 lib/javax.inject-2.5.0-b42.jar 隐藏