(Recovered from my old Blog).
Let’s say that you have an application which accepts a filepath as part of its interface. When unit testing, you may have some dummy files which you use to test the interface.
However, getting the full path of these test files can be a pain.
Assuming that the files are (as per Maven conventions) in /src/test/resources/ then they will be put on the classpath (by default /test-classes/ as part of the build process.
If you’re using Spring, then you can take advantage of that to do the following:
Resource classPathResource = new ClassPathResource("/simple_dataset.csv");
String inputFilename = classPathResource.getURI().getPath();
inputFilename now contains the full path to the file, and can be passed into your application as per normal.