Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 353
saddy dj Avatar asked May 04 '10 11:05

saddy dj


People also ask

How do you get the path of SRC test resources directory in JUnit?

File class to read the /src/test/resources directory by calling the getAbsolutePath() method: String path = "src/test/resources"; File file = new File(path); String absolutePath = file. getAbsolutePath(); System.

How do I add resources to SRC test?

Right click on maven project --->Click on Build Path ----->Click on New Source Folder. New source folder window will open, give the name to your folder example - src/test/source. click on Finish.

How do I run a JUnit test in Maven?

We can run our unit tests with Maven by using the command: mvn clean test. When we run this command at command prompt, we should see that the Maven Surefire Plugin runs our unit tests. We can now create a Maven project that compiles and runs unit tests which use JUnit 5.


1 Answers

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)
like image 189
Pascal Thivent Avatar answered Sep 22 '22 01:09

Pascal Thivent