【问题标题】:How to configure pom for StrutsTestCase so that web.xml is found?如何为 StrutsTestCase 配置 pom 以便找到 web.xml?
【发布时间】:2011-08-15 20:43:07
【问题描述】:

我正在使用 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 移动到src/main/webapp 时会发生什么?
  • 好的,我试过了,但没有帮助。谢谢你的想法。

标签: java unit-testing maven junit struts-1


【解决方案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>

【讨论】:

  • 我意识到这个问题只有 10 个月大,但我无法评估您的答案,无法访问相关代码库。所以我不会投票或接受,但也许其他人会感谢你的贡献。
【解决方案2】:

我认为最简单的方法是通过拥有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>

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

  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") );
  }

【讨论】:

  • 我以前没有使用过surefire-plugin。我尝试添加它,但它没有成功添加系统属性。但即使 that 起作用了,我也无法让 setServletConfigFile 使用字符串文字。如何将值传递给此方法的问题是次要的。
猜你喜欢
  • 1970-01-01
  • 2012-11-13
  • 2018-09-16
  • 2012-02-03
  • 1970-01-01
  • 2014-08-26
  • 1970-01-01
  • 2018-01-03
  • 2013-09-25
相关资源
最近更新 更多