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.
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.
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.
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.
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 testIf 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With