1

I have the following project structure (both project and module contain pom.xml files):

Image

Now, in the test class SomeTest.java I want to test some functionality using image.jpg in the resources folder.

I'm using the following file path as a constant:

private static final String IMG_PATH = "src/test/resources/image.jpg";

I then read in this as a file using

BufferedImage image = ImageIO.read(new File(IMG_PATH));

When running this project with the following Maven configuration, everything works fine.

Image

However, when running the unit test manually, the image cannot be found, because the file path is different.

Printing the file path shows the following as an absolute path:

C:\Users\Name\Documents\project\src\test\resources\image.jpg

As you can see, it's project/src and not project/module/src.

How can I configure JUnit so that it's root folder for resources is the Maven module folder and not the project folder?

I want to be able to run the tests using Maven as well as by hand for debugging.

jonasstr
  • 57
  • 3
  • 9

1 Answers1

1

Maven by default copies the (test) resources folder to the target folder which it then adds to its class path.

So what works is loading the file as a resource from the class path into a inputstream, and then feeding that input stream into ImageIO.read(java.io.InputStream).

try (InputStream inStream = SomeTest.class.getResourceAsStream('/image.jpg')) {
    if (inStream != null) {
        BufferedImage image = ImageIO.read(inStream);
        // ...
    }
}

Note that the / refers to the root of the class path on which the image is found. Also note that you usually should not refer to the src/ in your code, since at run time only the target folder is visible.

Also note that Eclipse (or IntelliJ) when setup properly takes the class path from maven, so this set up should also facilitate running the tests in the IDE for debugging.

avandeursen
  • 8,458
  • 3
  • 41
  • 51
  • Thank you, but it still doesn't work. I suspect it has something to do with the configuration settings. I have the following source folders for the java build path: `project/module/src/main/java` and `project/module/src/test/java`. Do you think these are correct? – jonasstr Apr 27 '19 at 12:47
  • All such settings should be standard in maven. Does it work when you run your tests from maven directly? – avandeursen Apr 27 '19 at 12:53
  • If it works in maven but not in Eclipse, this probably means that you did not tell Eclipse that it should view this project as a maven project, and that it should learn all dependencies and settings from the pom file. I think [this question](https://stackoverflow.com/questions/32399467/add-maven-nature-to-existing-eclipse-project-that-has-pom-but-no-maven-submenu) may help you in properly setting the maven nature in Eclipse. – avandeursen Apr 27 '19 at 13:21
  • 1
    I now tried it in IntelliJ and it works haha. Don't know why this is so hard in Eclipse. Thank you for your help! – jonasstr Apr 27 '19 at 13:24