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

时间:2011-08-15 20:43:07

标签: 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中应包含哪些资源。
  3. 我正在努力实现这一点,这是我目前的进展:

    测试类:

    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 */ }
    

    我在testResources的{​​{1}}标记中加入了以下build

    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>
    

    当然,我在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,没有不同的结果。

2 个答案:

答案 0 :(得分:1)

我正在使用默认的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>

答案 1 :(得分:0)

我认为最简单的方法是通过保存web.xml路径的属性。

这样您只需将属性值传递给surefire插件

即可
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
      <systemPropertyVariables>
        <test.web-xml.file>${project.build.directory}/MYPROJECT/WEB-INF/web.xml</test.web-xml.file>
      </systemPropertyVariables>
    </configuration>
  </plugin>

然后在你的setup方法中获取一个系统属性:

  public void setUp() throws Exception {
    super.setUp();
    // You may want to do some validation if this property is set and
    // points to a file
    setServletConfigFile( System.getProperty("test.web-xml.file") );
  }