Junit run not picking file src/test/resources. For file required by some dependency jar
Asked Answered
P

2

7

I m facing a issue where test/resource is not picked,but instead jar's main/resource is picked

Scenario is like : Myproject src/test/resources--- have config.xml w which should be needed by abc.jar which is a dependecy in Myproject.

When running test case for Myproject its loading config.xml of abc.jar instead of Myproject test/resources. - I need to know order in which maven pick resources. - Or wat im trying is not possible.

Thanks.

Pisolite answered 4/5, 2010 at 11:13 Comment(1)
Rephrasing question : Myproject dependency abc.jar class loadFile.java loads config.xml file which under abc src/main/resource. When i m running Myproject test case which .xml loadFile.java should pick. 1. abc jar main/resource or 2. Myproject tase/resourcePisolite
P
13

Files from target/tests-classes (by default) are included at the beginning the test classpath. So when running tests, resources from both src/main/resources and src/test/resources are on the classpath but the later has precedence over the former. In other words, if you have a config.xml in src/main/resources and in src/test/resouces:

  • src/main/resources/config.xml will be packaged in the final artefact but
  • src/test/resources/config.xml will be used when running test

If this is not what you're experiencing, there must be a mistake somewhere else.

If you want to convince yourself you can run mvn -X test, this will print the Test Classpath. And you'll see that this classpath is made of (in this order):

  • target/test-classes
  • target/classes
  • the project jar
  • the dependencies (including those with a test scope)
Pothead answered 4/5, 2010 at 11:35 Comment(5)
Thanks for reply... its showing as wat u mention. This means config.xml file in test-classes will get preference over config.xml file in some dependencies jar. But this not happening in my case dependency jar is loading its own .xml rather my test/resources. Wat i can do to trace it.Pisolite
@saddy-dj Who is loading config.xml exactly? A test case or a class of a JAR? I think I start to understand what might be the cause of the problem...Pothead
@saddy-dj What code do you use to load the file? Can you update your question with the code snippet?Pothead
Inside abc.jar loadFile.java call fileLoderClass.java getFile()mehtod [fileLoderClass inside someOther.jar] fileLoderClass loads file in this way. inStream =this.getClass().getClassLoader().getResourceAsStream(this.configFileName); config file present in abc.jarPisolite
This is an excellent thing to understand, because for instance log4j.xml could be in the /src/test/properties of the dependent jars but not in /src/main/resources, and when you build using the maven-assembly-plugin it will only pick up the one in the final /src/main/resources and not the others, but their tests can still run (and log).Eleaseeleatic
L
0

I ran into a similar issue with my project. So I have TestClassA in ProjectA that calls ClassB from ProjectB. ClassB uses a file in src/test/resources. For some reason this file was not being used. I found out that ProjectA had a file by the same name in its src/test/resources.
So in summary, even though ClassB was in ProjectB, it was using ProjectA's src/test/resources because that's the project where the test originated.

Lander answered 19/6, 2013 at 20:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.