Junit 运行不选择文件 src/test/resources。对于某些依赖jar所需的文件
我面临的问题是未选择测试/资源,而是选择了 jar 的主要/资源
场景如下:Myproject src/test/resources--- 有 config.xml w abc.jar 应该需要它,它是 Myproject 中的依赖项。
当为 Myproject 运行测试用例时,它加载 abc.jar 的 config.xml 而不是 Myproject test/resources。 - 我需要知道 Maven 选择资源的顺序。 - 或者我尝试是不可能的。
谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自
target/tests-classes
的文件(默认情况下)包含在测试类路径的开头中。因此,在运行测试时,来自 src/main/resources 和 src/test/resources 的资源都位于类路径上,但后者优先于前者。换句话说,如果您在src/main/resources
和src/test/resouces
中有一个config.xml
:src /main/resources/config.xml
将被打包在最终的artefact中但是src/test/resources/config.xml
将在运行测试时使用如果这不是您遇到的情况,一定是其他地方出现了错误。
如果您想说服自己可以运行
mvn -X test
,这将打印测试类路径。您将看到该类路径由以下部分组成(按此顺序):target/test-classes
target/classes
Files from
target/tests-classes
(by default) are included at the beginning the test classpath. So when running tests, resources from bothsrc/main/resources
andsrc/test/resources
are on the classpath but the later has precedence over the former. In other words, if you have aconfig.xml
insrc/main/resources
and insrc/test/resouces
:src/main/resources/config.xml
will be packaged in the final artefact butsrc/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
我的项目遇到了类似的问题。所以我在 ProjectA 中有 TestClassA,它从 ProjectB 调用 ClassB。 ClassB 使用 src/test/resources 中的文件。由于某种原因,该文件未被使用。我发现ProjectA的src/test/resources中有一个同名的文件。
总而言之,即使 ClassB 在 ProjectB 中,它也使用 ProjectA 的 src/test/resources,因为那是测试发起的项目。
I ran into a similar issue with my project. So I have TestClassA in ProjectA that calls ClassB from ProjectB. ClassB uses a file in src/test/resources. For some reason this file was not being used. I found out that ProjectA had a file by the same name in its src/test/resources.
So in summary, even though ClassB was in ProjectB, it was using ProjectA's src/test/resources because that's the project where the test originated.