Junit运行时未选择文件src/test/resources.对于某些依赖项jar所需的文件 [英] Junit run not picking file src/test/resources. For file required by some dependency jar

查看:198
本文介绍了Junit运行时未选择文件src/test/resources.对于某些依赖项jar所需的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临一个问题,即未选择测试/资源,而是选择了jar的主/资源

I m facing a issue where test/resource is not picked,but instead jar's main/resource is picked

场景就像:Myproject src/test/resources ---具有config.xml w 这是Myproject中的一个依赖项abc.jar应该需要的.

Scenario is like : Myproject src/test/resources--- have config.xml w which should be needed by abc.jar which is a dependecy in Myproject.

为Myproject运行测试用例时,其加载abc.jar的config.xml而不是Myproject测试/资源. -我需要知道Maven选择资源的顺序. -否则无法尝试.

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.

谢谢.

推荐答案

来自target/tests-classes的文件(默认情况下)包含在开始测试类路径中.因此,在运行测试时,来自src/main/resourcessrc/test/resources的资源都在类路径上,但是后者优先于前者.换句话说,如果您在src/main/resourcessrc/test/resouces中具有config.xml:

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将被包装在最终产品中,
  • src/test/resources/config.xml将在运行测试时使用
  • 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.

如果您想说服自己可以运行mvn -X test,则将打印测试类路径.您会看到该类路径是由(按此顺序)构成的:

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
  • 项目jar
  • 依赖关系(包括具有测试范围的依赖关系)
  • target/test-classes
  • target/classes
  • the project jar
  • the dependencies (including those with a test scope)

这篇关于Junit运行时未选择文件src/test/resources.对于某些依赖项jar所需的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆