How to determine what resources are available for tests using Maven?
The context for my problem is given here: How to configure pom for StrutsTestCase so that web.xml is found?
In my pom.xml
I have included the following, with hopes of making various files available during tests:
<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>
My expectation is that everything in 开发者_如何学JAVAsrc/test/java
will be available during tests, as will be web.xml
, struts-config.xml
in webapp/WEB-INF
.
The web.xml
file is not being found during tests. In troubleshooting this I have two problems.
- I'm not completely sure where I am (or should be) telling my other code to look for
web.xml
- I have no idea how to verify that
web.xml
is available while tests are being run.
For now, I'm asking #2. How can I verify that the XML above is doing what I think it should, and making web.xml
available for use in tests?
I think the problem with your configuration may be that you've included the "WEB-INF
" as part of your <testResource>
. What you probably want is something more like this:
<testResources>
<testResource>
<directory>webapp</directory>
</testResource>
</testResources>
(Or, if you're using the Standard Directory Layout as mentioned here, the directory
would instead be src/main/webapp
.)
And, if you only want to include XML files in the WEB-INF directory, then you'll need some additional "includes
" config:
<testResources>
<testResource>
<directory>webapp</directory>
</testResource>
<includes>
<include>WEB-INF/*.xml</include>
</includes>
</testResources>
In order to test that these files are actually available on the classpath during your test runs, you can do something like follows:
URL webXml = getClass().getClassLoader().getResource("WEB-INF/web.xml");
assumeNotNull(webXml);
(As mentioned here, the use of assumeNotNull
is optional.)
Unless you need the deployment descriptors (i.e web.xml
, struts-config.xml
, etc.) on the test classpath itself (e.g. to be be looked up via classloader.getResource*
), then there should be no need for you to add or modify the testResources
in the POM. Try simply looking up the file in your test setup as follows:
File file = new File("webapp/WEB-INF/web.xml");
assumeTrue(file.exists());
(Note: The use of assumeTrue
may be used to ensure that the file exists as a precondition prior continuing to execute the tests, but this method can be replaced as desired.)
I think you need to change the path to the xml files to include them in the path for your tests.
Try:
<testResource>
<directory>src/main/webapp/WEB-INF</directory>
<includes>
<include>*.xml</include>
</includes>
</testResource>
精彩评论