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.

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
saddy dj
  • 147
  • 1
  • 3
  • 13
  • 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/resource – saddy dj May 04 '10 at 13:12

2 Answers2

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)
Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
  • 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. – saddy dj May 04 '10 at 12:42
  • @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... – Pascal Thivent May 04 '10 at 12:47
  • @saddy-dj What code do you use to load the file? Can you update your question with the code snippet? – Pascal Thivent May 04 '10 at 13:05
  • 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.jar – saddy dj May 04 '10 at 13:20
  • 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). – ggb667 Apr 10 '14 at 17:55
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.

LConrad
  • 816
  • 1
  • 11
  • 20