DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Integrate Cucumber in Playwright With Java
  • OpenAPI From Code With Spring and Java: A Recipe for Your CI
  • Apache Spark 3 to Apache Spark 4 Migration: What Breaks, What Improves, What's Mandatory
  • Web App Load Testing Using Maven Plugins for Apache JMeter, and Analyzing the Results

Trending

  • Migrate a Hardcoded LangGraph Agent to LaunchDarkly AI Configs in 20 Minutes
  • Why DDoS Protection Is an Architectural Decision for Developers
  • When One MVP Is Really Four Systems: A Better Way to Plan Multi-Role Apps
  • OpenAPI From Code With Spring and Java: A Recipe for Your CI
  1. DZone
  2. Coding
  3. Java
  4. Organizing Resource Files for Tests Using Maven and Java

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.

By 
Alan Richardson user avatar
Alan Richardson
·
Dec. 19, 17 · Tutorial
Likes (9)
Comment
Save
Tweet
Share
48.0K Views

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.

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.

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 and test
  • use the target folder hierarchy to check if files are in the correct place
Testing Java (programming language) Apache Maven

Published at DZone with permission of Alan Richardson. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Integrate Cucumber in Playwright With Java
  • OpenAPI From Code With Spring and Java: A Recipe for Your CI
  • Apache Spark 3 to Apache Spark 4 Migration: What Breaks, What Improves, What's Mandatory
  • Web App Load Testing Using Maven Plugins for Apache JMeter, and Analyzing the Results

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook