The cleanest way to get a file in Maven's JUnit test case

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

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

The above test script works fine when I start a project from a child (i.e. level B), but when I start from a parent project A (i.e., by performing mvn installation at the parent level), it fails, because obviously not folders called "src / main / webapp" under the parent level (however, it is available at the child level).

I know that I can do some encoding, check if the test case works from the parent / child module, but apparently I want to know what others did when they had this problem?

And no, I cannot use classpath instead (for various reasons).

I also tried the relative path, but then the type of test case starts to know too much. Is there any solution for this?

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

I tried "packageExcludes" and "webResources / excludes" and got the war plugin to omit the second web.xml, but still the console-webapp directory is copied (although it is empty). Is there a way to tell the maven war plugin to completely ignore the directory (i.e. What is the ant pattern for this?)

+4
source share
2 answers

The Maven 2 conventions state that all test resources, including files, such as the ones you are trying to use in your test, should be stored in the src/test/resources folder. If you abide by this agreement, you can directly get your file from the class. 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 pom.xml :

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

In your question, you indicated that you cannot use the classpath. Why is that?

+7
source

Is the file you are trying to access the test path to? If so, what 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; } } 
+2
source

Source: https://habr.com/ru/post/1300987/


All Articles