views:

310

answers:

2

Hi, 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.

A: 

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
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 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
class of a JAR is loading it...
@saddy-dj What code do you use to load the file? Can you update your question with the code snippet?
Pascal Thivent
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
A: 

I used this tag in my pom. never

As i find that test class set by maven but by default test cases run as few fork process with -classpath aragument. Forking command line:

it worked... But not able to understand the reason correctly. Can any once plz explain that.