Reading Resource Files in Maven Tests

Posted: June 10, 2014 in Random Posts

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

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s