Over a million developers have joined DZone.

Organizing Resource Files for Tests Using Maven and Java

DZone 's Guide to

Organizing Resource Files for Tests Using Maven and Java

We take a look at how to organize your resource files for testing in your Java application by making use of Maven. Here's how to ensure your hierarchy works.

· Java Zone ·
Free Resource

Resources are a very useful concept in Java. They are essentially files in projects that are compiled into your 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 StackOverflow.

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 the resources is my package hierarchy.

For example, I have a test called LinkCheckerTest and it is in a package called:

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

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 theLinkCheckerTest using:

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

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

How to Check Whether You Have Organized This Properly

Many of the queries online 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.

For example, if I look in my target folder to find the linksToCheck.txt file, if I have organized everything correctly, then the file should be in the same folder as the class.

Resource File In Test Class Folder Hierarchy

In the image above, I can see that it is, which means that the getResource method 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 the relative path to access it — for example, 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
java ,testing ,maven ,resource hierarchy ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}