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

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Integrate Cucumber in Playwright With Java
  • Using Lombok Library With JDK 23
  • Selenium Grid Tutorial: Essential Tips and How to Set It Up
  • Automating Cucumber Data Table to Java Object Mapping in Your Cucumber Tests

Trending

  • Automating Data Pipelines: Generating PySpark and SQL Jobs With LLMs in Cloudera
  • The Role of Functional Programming in Modern Software Development
  • AI-Based Threat Detection in Cloud Security
  • Teradata Performance and Skew Prevention Tips
  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
47.4K 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, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Integrate Cucumber in Playwright With Java
  • Using Lombok Library With JDK 23
  • Selenium Grid Tutorial: Essential Tips and How to Set It Up
  • Automating Cucumber Data Table to Java Object Mapping in Your Cucumber Tests

Partner Resources

×

Comments

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: