Junit 运行不选择文件 src/test/resources。对于某些依赖jar所需的文件

发布于 2024-08-30 22:20:18 字数 267 浏览 9 评论 0原文

我面临的问题是未选择测试/资源,而是选择了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枫林﹌晚霞¤ 2024-09-06 22:20:18

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

  • src /main/resources/config.xml将被打包在最终的artefact中但是
  • src/test/resources/config.xml将在运行测试时使用

如果这不是您遇到的情况,一定是其他地方出现了错误。

如果您想说服自己可以运行mvn -X test,这将打印测试类路径。您将看到该类路径由以下部分组成(按此顺序):

  • target/test-classes
  • target/classes
  • 项目 jar
  • 依赖项(包括带有测试的依赖项)范围)

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)
枯叶蝶 2024-09-06 22:20:18

我的项目遇到了类似的问题。所以我在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文