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.
Join the DZone community and get the full member experience.
Join For Free
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 the
linkcheckertest
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.
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.
summary
- resources are added to the class path hierarchy
- class path is relative to the package of the class
-
resource paths are split between
main
andtest
-
use the
target
folder hierarchy to check if files are in the correct place
Published at DZone with permission of Alan Richardson, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments