Reading Resource Files in Maven Tests

Artifex Anima

Sometimes when writing tests in a maven project we need to read the contents of a resource file. These resource files are located in the src/test/resource folder of the project. One way to do this is (other way):

The getResourceAsStream method returns a InputStream to read the contents of the file with the name fileName in the src/test/resource folder. The slash (‘/’) indicates that fileName is the absolute path, i.e, it will open a file named src/test/resource/fileName. If, instead, we did not specified the slash (‘/’), then the file opened would be src/test/resource/modified_package_name/fileName. Where modified_package_name is the package name of the object this with ‘/’ substituted for ‘.’.

The returned stream is used as you would normally use a FileInputStream. You can use a BufferedReader to read lines as Strings, for instance:

This code works with the Maven Surefire Plugin and the Jenkins continuous…

View original post 2 more words

Leave a comment

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