Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load test resources within other module?

Tags:

java

junit

maven

I'm using a abstract class in another module for reading and input for my testdata with:

package src/main/java/path/to/my/base/testclass;
InputStream stream = getClass().getResourceAsStream(filename);

filename is eg "test.txt", located in src/main/resources/path/to/my/base/testclass

As long as I put this abstract class into the same module as my testclasses are in, everything works fine. Then i extract the acstract class (as well as the resources) to other module, compile, add to pom etc. Result: My test implementation runs fine, but: I'm getting IO exception as the file could not be found.

What am I missing here? Why does the abstract class work within the same module, but not within another?

like image 234
membersound Avatar asked Nov 03 '22 21:11

membersound


1 Answers

Test resources are for this artifact's tests only, they don't get deployed.

There are two possible ways around this:

  • Dirty: Make your app deploy a test jar along with the main jar, and add that as a dependency with scope TEST to the second artifact.
  • Clean: Create a separate test artifact for base test classes and common test resources. Important: in this artifact, nothing goes in src/test and everything goes in src/main. Reference this test artifact from both other artifacts with scope TEST.
like image 52
Sean Patrick Floyd Avatar answered Nov 08 '22 09:11

Sean Patrick Floyd