问题
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:
- If your
web.xml
file is in a non-standard location, thensetServletConfigFile()
can be used to indicate toStrutsTestCase
where the file is. - 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:
Test class:
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 */ }
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>
Here is the structure of the project:
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
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.
回答1:
I'm using the default Maven webapp structure
myproject
|---src
| |---main
| | |---java
| |
| |---test
| | |---Java
| |
| |---webapp
| |---WEB-INF
| |---struts.config.xml
| |---web.xml
|
|---pom.xml
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>
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>
回答2:
I think the easiest way to do this is through property that holds a path to web.xml
.
This way you can simply pass the property value to surefire plugin
<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>
Then in your setup method just get a system property:
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") );
}
来源:https://stackoverflow.com/questions/7070611/how-to-configure-pom-for-strutstestcase-so-that-web-xml-is-found