How to load files from Spring JUnit test resources folder

In this short tutorial we will show you how to read the files from the /src/test/resources directory path.

It is common practice for unit testing in the Spring JUnit test to load files and their content to make more robust test cases. Or we have a test case where we need perhaps read or write data or test specific files in general.

1. Using ClassLoader

First option is to use and instance of the ClassLoader.

final ClassLoader classLoader = getClass().getClassLoader();
final String resourceName = "daily_report.csv";
final File file = new File(classLoader.getResource(resourceName).getFile());
final String absolutePath = file.getAbsolutePath();

System.out.println(absolutePath);

Assert.assertTrue(absolutePath.endsWith("/daily_report.csv"));

We will get the output of the absolute path as:

/Users/your.user.name/DEV/tutorials/testing-modules/junit-5-basics/target/test-classes/daily_report.csv

Be aware that the path to the resource will look slightly different from the rest of the examples. It is because the ClassLoader looks for the resources on the classpath. Therefore obtaining a path to the resource is from the point of the compiled project.

If you build your project with Maven, compiled classes and resources are in the /target directory in the project root folder. That is why you need to count on the path to resource adjusted for the difference. The other two methods use project root.

2. Using Path

Second option is to use an instance of the Path class. We will call a static factory method Paths.get() which will return us its new instance. Then we will convert Path to File instance and from the that we will extract absolute path with getAbsolutePath() method.

final Path resourcePath = Paths.get("src","test","resources");
final String absolutePath = resourcePath.toFile().getAbsolutePath();

System.out.println(absolutePath);

Assert.assertTrue(absolutePath.endsWith("src/test/resources"));

We will get the same output as in example using java.io.File:

/Users/your.user.name/DEV/tutorials/testing-modules/junit-5-basics/src/test/resources

Path class was introduced in Java 7; consequently, it is available also in older and legacy projects.

3. Using java.io.File

Third approach is to use an instance of the java.io.File class to read the /src/test/resources directory. We will create instance and then we will call its method getAbsolutePath() method. Path we will obtain in this way is relative path towards the current working directory of the project.

final String resourcePath = "src/test/resources";
final File file = new File(resourcePath);
final String absolutePath = file.getAbsolutePath();

System.out.println(absolutePath);

Assert.assertTrue(absolutePath.endsWith("src/test/resources"));

Let’s see an example output when running on the Unix based system:

/Users/your.user.name/DEV/tutorials/testing-modules/junit-5-basics/src/test/resources

Conclusion

This article was a quick tutorial where we showed how to read resources from the /src/test/resources directory in JUnit 5.

There are three different ways how to get a resource for testing. We can either use File, Paths or ClassLoader class.

As always, you can find all our examples on our GitHub project!

This entry was posted in Tutorials and tagged , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.