Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8170777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:14:31+00:00 2026-06-06T21:14:31+00:00

I am having trouble getting unit tests to use resources provided under src\test\resources instead

  • 0

I am having trouble getting unit tests to use resources provided under src\test\resources instead of those in src\main\resources. Yes, the resource in question is named identically in both places.

I have done a fair amount of research, looked at StackOverflow posts such as:

  • Make Maven use test resources
  • Make Eclipse use test resources

However this basic issue has me stumped.

I have a very standard Maven Java project set up:

  • Application source & resources under src\main
  • Test source & resources under src\test

Eclipse (thanks to M2Eclipse plugin) has src\main\java, src\main\resources, src\test\java and src\test\resources all in its build path.

With this setup, when I run my unit test from within Eclipse resource files in src\main\resources are being referenced. I can see why (because they are in the build path) but I don’t know how to prevent it. The unit test configuration panel does not allow me to tweak the order of (default) classpath components.

Even if I were to gloss over my inability to run my tests from Eclipse, Maven doesn’t play nice either. I can see that the processResources step is copying all the test resources to under the target\test-classes tree. However when the unit test is executed the copy of the resource file under target\classes is being consulted.

Surely I am overlooking something obvious. I appreciate any help you can provide.

[EDIT] Adding more (possibly relevant information):

The resource file in question is a file named persistence.xml. It is used by Eclipselink to connect to relational data sources. The version of the file used by the application is placed in src\main\resources\META-INF. A Maven build (of a WAR, say) places the META-INF directory under the classes directory. Therefore EclipseLink tooling finds this file.

src/main
  |
  + -- java/com/company/ProductDao.java
  | 
  + -- resources/META-INF/persistence.xml  (Requires application container)
  |
src/test
  |
  + -- java/com/company/ProductDaoTest.java
  | 
  + -- resources/META-INF/persistence.xml  (out of container for unit tests)
  |

The takeaway here is: EclipseLink requires a file called persistence.xml in a META-INF directory on the classpath (yes, defaults can be overridden but let’s not go there).

Therefore when the unit test runs the same application logic is invoked. However the version of the file in src\main\resources cannot be used. A different version is made available in the src\test\resources directory.

Since the version in src\main\resources is being used it appears that my only recourse is to instruct Eclipselink during unit testing to look for a different filename.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T21:14:35+00:00Added an answer on June 6, 2026 at 9:14 pm

    This isn’t a Maven issue at the root of it. You’d have the exact same problem if you had everything collapsed into a single source folder. Think about what you are really doing. Your unit tests require the code from src/main or else it wouldn’t be a unit test of that code. src/main/java and src/main/resources make up the artifact you are testing. You don’t mix and match the directories for your unit test.

    You are basically doing this:

    /src/com/mycompany/myresource.txt        
    /src/com/mycompany/MyAwesomeClass.java
    /src/com/mycompany/myresource.txt
    /src/com/mycompany/TestMyAwesomeClass.java
    

    Don’t do that. Configure your unit tests to load mock data / your resource file from another file.

    I haven’t had many, if any issues with Maven and Eclipse. Realize that Maven is not magic. And that if you are going to use Maven and Eclipse everything has to be driven by the POM file not the other way around. Then things work beautifully.

    Just to reclarify, your issue has nothing to do with Maven. You are declaring a resource twice on the classpath. You need to rethink your unit tests. There are things you can do to get around this with Profiles but it’s an abuse of the build system.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having some trouble getting NHV to work with Fluent NHibernate. A unit test
I'm having some troubles getting my unit tests to be setup to use an
having trouble getting my directx going I get the following error 1>Linking... 1>main.obj :
I am having trouble getting to use the refresh token. Looking at the configuration
im having trouble getting a string (containing a URL) from one activity to another.
Im having trouble getting my JTable that im using to display either check boxes
I'm having trouble getting the CanExecute method of my command to work property. I've
I'm having trouble getting Tire working using ElasticSearch with the Bonsai addon on the
I'm currently having trouble getting example code for using tweepy to access Twitter's Streaming
I'm having trouble getting Japanese text to display properly on a website compiled using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.