Skip to main content
blog title image

3 minute read - FAQ Maven Java For Testers

How to organise resource files for tests using Maven and Java

Dec 7, 2017

Resources are a very useful concept in Java. They are essentially files in the project which are compiled into the .jar

Java also has commands for finding them .getResource and reading them .getResourceAsStream

Very Handy.

But, they can be hard to wrap your head around. As evidenced by my own experiences trying to use them and the number of queries on stack overflow.

What is a Resource?

A resource is a file in the class path folder structure for your project.

This is important because your test resources will be put in your test-classes folder hierarchy and your main resources will be put in your classes folder hierarchy - both in your target folder.

How to Create a Resource Folder Structure

Maven has a standard directory layout.

We make things easy for ourselves by sticking to this layout.

I can have two obvious resource folder hierarchies:

  • src\main\resources - for resources available to my main ’live’ code
  • src\test\resources - for resources available to my test code

The folder hierarchy below resources is my package hierarchy.

i.e. if I have a test called LinkCheckerTest and it is in a package called

package com.javafortesters.course.exercises.casestudy.casestudy_002_buildAnHttpLinkChecker;

Appologies for the length, this is from an example in my Face to Face Java For Testers Training Course

The full folder structure for a resource file used by the LinkCheckerTest would be:

- src
 - test
  - resources
   - com
    - javafortesters
     - course
      - exercises
       - casestudy
        - casestudy_002_buildAnHttpLinkChecker

I could access a resource in the folder structure from within the LinkCheckerTest using

URL fileToRead = LinkCheckerTest.class.getResource("linksToCheck.txt");

Where linksToCheck.txt is a file in the casestudy_002_buildAnHttpLinkChecker folder listed above.

How to check if you have organised this properly?

Many of the queries on line are about .getResource not finding the file.

getResource looks for the file, relative to the Class in the class hierarchy. This means that if you look in the target folder to find your resource file then you will see if you have it in the correct place.

i.e. if I look in my target folder to find the linksToCheck.txt file and if I have organised everything correctly then the file should be in the same folder as the class.

Resource File In Test Class Folder Hierarchy

In the above image I can see that it is, which means that the getResource method earlier will work.

I know this because the files are in the same folder:

- target
 - test-classes
   - com
    - javafortesters
     - course
      - exercises
       - casestudy
        - casestudy_002_buildAnHttpLinkChecker
           - LinkCheckerTest.class
           - linksToCheck.txt

If the linksToCheck.txt file was in a different place in the target folder then I would have to use relative path to access it e.g. I would use ../linksToCheck.txt if I had placed the file in the casestudy folder rather than the casestudy_002_buildAnHttpLinkChecker folder.


  • resources are added to the class path hierarchy
  • class path is relative to the package of the class
  • resource paths are split between main and test
  • use the target folder hierarchy to check if files are in the correct place