One of my unit tests is driven by test data, stored in src/test/resources/testData.txt. Since Maven will copy the contents of src/test/resources to target/test-classes/ which is on the classpath, I can read that file conveniently from the classpath without caring about its absolute file name. So far, so good.
Now I’m in the process of witing some code that will update that file with data fetched from a remote system. That code will be run rarely, and not as part of the build. The updated file will be committed to the source repository. In order to append to the file, I need to know the path to the original file in src/test/resources (appending to the one in target/test-classes/ will obviously not work).
How do I determine that path in a portable way? I’d rather not hard code src/test/resources/, because those directory names may actually change. Also, this would break if executed with a current working directory other than my project base directory.
Any ideas?
The best way would probably be with
${project.basedir}/src/test/resources. The test resource directory is unlikely to change, and it will safely resolve to the correct path regardless of your working directory. Trying to reference a test resource directory via a property is problematic because there can be, and often are, multiple test resources defined. How would you know which one to pick?