Junit does not start src / test / resources file collection. For the file required by some dependency ban

I am facing a problem when a test / resource is not selected, but the main / resource jar is selected instead

The scenario is similar: Myproject src / test / resources --- there is config.xml w which should be needed by abc.jar, which is a dependency in Myproject.

When running a test case for Myproject, the config.xml file abc.jar is loaded instead of the Myproject test / resources. - I need to know the order in which maven selects resources. β€œOr trying wat im is not possible."

Thanks.

+7
junit maven-2 resources testing surefire
source share
2 answers

Files from target/tests-classes (by default) are included at the beginning of the test path. Therefore, when running tests, resources from src/main/resources and src/test/resources are in the class path, but the later one takes precedence over the first. In other words, if you have config.xml in src/main/resources and in src/test/resouces :

  • src/main/resources/config.xml will be packaged into the final artifact , but
  • src/test/resources/config.xml will be used when starting the test

If this is not what you are experiencing, there must be a mistake elsewhere.

If you want to convince yourself, you can run mvn -X test , this will print a test path. And you will see that this class path consists of (in this order):

  • target/test-classes
  • target/classes
  • project container
  • dependencies (including those that have a scope of validation)
+13
source share

I had a similar problem with my project. Therefore, I have TestClassA in ProjectA that calls ClassB from ProjectB. ClassB uses the file in src / test / resources. For some reason this file was not used. I found out that ProjectA has a file with the same name in its src / test / resources.
So, overall, although ClassB was in ProjectB, it used ProjectA src / test / resources, because the project in which the test was tested.

0
source share

All Articles