如何为StrutsTestCase配置pom以便找到web.xml? [英] How to configure pom for StrutsTestCase so that web.xml is found?

查看:91
本文介绍了如何为StrutsTestCase配置pom以便找到web.xml?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Maven 2来构建Java-Sturts 1.2-Spring 1.2项目.我正在尝试合并JUnit的StrutsTestCase扩展,以允许对动作类进行测试.

I'm using Maven 2 to build a Java - Sturts 1.2 - Spring 1.2 project. I'm trying to incorporate the StrutsTestCase extension of JUnit to allow testing of action classes.

当我尝试构建时,出现以下错误:

When I try to build, I get the following error:

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)

通过研究,我学到了以下内容:

In researching this, I've learned the following:

  1. 如果web.xml文件位于非标准位置,则setServletConfigFile()可用于指示StrutsTestCase文件的位置.
  2. 使用Maven进行构建时,可能有必要指出pom.xml中应包含哪些资源.
  1. If your web.xml file is in a non-standard location, then setServletConfigFile() can be used to indicate to StrutsTestCase where the file is.
  2. When building with Maven, it may be necessary to indicate what resources should be included in the pom.xml.

我正在尝试实现这一点,这是我当前的进度:

I'm trying to implement this, here is my current progress:

测试类:

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.

I have included the following testResources in the build tag in the 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.xmlsetServletConfigFile中各种路径的变体.我也尝试过使用setContextDirectory,结果没有变化.

Of course, I've tried a variety of variations of the paths in pom.xml and in setServletConfigFile. I've also tried using setContextDirectory, with no different result.

推荐答案

我正在使用默认的Maven webapp结构

I'm using the default Maven webapp structure

myproject
|---src
|   |---main
|   |   |---java
|   |
|   |---test
|   |    |---Java
|   |
|   |---webapp
|        |---WEB-INF
|             |---struts.config.xml
|             |---web.xml
|
|---pom.xml

我正在将Webapp XML文件添加到测试资源中,并且无需调用setServletConfigFile方法.

I'm adding webapp XML files to my test resources and I don't need to call setServletConfigFile method.

<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文件中添加以下行:

So, in your case, you should add these lines in your POM file :

<project>
...
<build>
...
<testResources>
    <testResource>
        <directory>webapp/WEB-INF</directory>
        <targetPath>/WEB-INF</targetPath>
        <includes>
            <include>*.xml</include>
        </includes>
    </testResource>
</testResources>
..
</build>
...
</project>

这篇关于如何为StrutsTestCase配置pom以便找到web.xml?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆