开发者

Cleanest way to get a File in a JUnit test case from Maven

My test code in Maven project B (which is a child of project A) has something like

String filePath = "src/main/webapp";
//do something with the filePath

The above test case runs fine when I run the project from child (i.e, level B) but when I run from Parent project A (i.e, doing a mvn install at parent level) this fails because obviously there is no folder called "src/main/webapp" under parent level (It is however available in child level).

I know I could do some coding do check if a test case is running from parent/child module but apparently I want to know what others have done when they have had this problem ?

And no, I cant use the classpath instead (for various boring reasons).

I also tried relative path but then the test case sort of starts to know too much. Is there actually a solution at all for this ?

UPDATE (12/Feb) - I have a web.xml under webapp folder and create a jetty server in a test case using that web.xml. Ideally src/main/webapp is not placed in the classpath. It is used by the WAR plugin to package the WAR. Now, I tried an alternative where I put my web.xml in the src/main/resource/console-webapp/WEB-INF/web.xml directory and altered the webXml attribute in the maven war plugin. This seems to solve my problem. However, in the output WAR I have a web.xml and another redundant web.xml (which was copied because it is in the classpath).

I have tried "packageExcludes" and "webResources/exclu开发者_JAVA技巧des" and got the war plugin to omit the second web.xml but still the directory "console-webapp" gets copied (although is empty). Is there any way to tell maven war plugin to ignore a directory completely (i.e, what is the ant pattern for that?)


The Maven 2 conventions state that all test resources, including files such as the one you are trying to use in your test, must be stored in the src/test/resources folder. If you respect this convention, you will be able to get your file from the classpath directly. These resources will not be included in the final packaging (JAR, WAR, EAR...).

Of course, you can change the directory and then specify the new test resources directory in your pom.xml:

<build>
    <testResources>
        <testResource>
            <directory>...</directory>
        </testResource>
    </testResources>
    ...

In your question, you specified that you can't use the classpath. Why so?


Is the file you are trying to access on the test classpath? If so, how about:

public class IOUtils {

  public URL getResourceAsURL(String resource) {
    ClassLoader cl = getClass().getClassLoader();
    return cl.getResource(resource);
  }


  public static InputStream getResourceAsStream(String resource) throws IOException  {
    ClassLoader cl = getClass().getClassLoader();
    InputStream in = cl.getResourceAsStream(resource);

    if (in == null) 
      throw new IOException("resource \"" + resource + "\" not found");

    return in;
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜