在 Eclipse 和 Maven 中使用 JUnit 时出现类路径问题

发布于 2024-09-02 20:24:41 字数 300 浏览 5 评论 0原文

在 JUnit 测试中,我使用此代码加载特定于测试的配置文件:

InputStream configFile = getClass().getResourceAsStream("config.xml");

当我通过 eclipse 运行测试时,它要求 xml 文件与测试文件位于同一目录中。

当我使用 maven 构建项目时,它要求 xml 位于 src/test/resources 中,以便将其复制到 target/test-classes 中。

我怎样才能让它们都只使用一个文件?

In a JUnit test I'm using this code to load in a test-specific config file:

InputStream configFile = getClass().getResourceAsStream("config.xml");

When I run the test through eclipse, it requires the xml file to be in the same directory as the test file.

When I build the project with maven, it requires the xml to be in src/test/resources, so that it gets copied into target/test-classes.

How can I make them both work with just one file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

自找没趣 2024-09-09 20:24:41

将 config.xml 文件放在 src/test/resources 中,并将 src/test/resources 添加为 Eclipse 中的源文件夹。

另一个问题是 getResourceAsStream("config.xml") 如何处理包。如果调用此函数的类位于 com.mycompany.whatever 包中,则 getResourceAsStream 也期望 config.xml 位于同一路径中。但是,这是类路径中的相同路径,而不是文件系统中的路径。您可以将文件放在 src/test/resources 下的同一目录结构中 - src/test/resources/com/mycompany/whatever/config.xml - 或者您可以在路径中添加前导“/” - 这使得 getResourceAsStream 从类路径的基部加载文件 - 因此,如果将其更改为 getResourceAsStream("/config.xml"),您可以将文件放入 src/test/resources /config.xml

Place the config.xml file in src/test/resources, and add src/test/resources as a source folder in Eclipse.

The other issue is how getResourceAsStream("config.xml") works with packages. If the class that's calling this is in the com.mycompany.whatever package, then getResourceAsStream is also expecting config.xml to be in the same path. However, this is the same path in the classpath not the file system. You can either place file in the same directory structure under src/test/resources - src/test/resources/com/mycompany/whatever/config.xml - or you can add a leading "/" to the path - this makes getResourceAsStream load the file from the base of the classpath - so if you change it to getResourceAsStream("/config.xml") you can just put the file in src/test/resources/config.xml

猫九 2024-09-09 20:24:41

尝试将 src/test/resources/ 目录添加为 Eclipse 项目中的源文件夹。这样,当 Eclipse 尝试运行单元测试时,它应该位于类路径上。

Try adding the src/test/resources/ directory as a source folder in your Eclipse project. That way, it should be on the classpath when Eclipse tries to run your unit test.

虚拟世界 2024-09-09 20:24:41

我知道这是一个老问题,但我偶然发现了它,并发现上面的答案都没有提到 Maven Eclipse 插件,这很奇怪。运行mvn eclipse:eclipse,它将添加src/main/resourcessrc/test/resources等到“Java构建路径”在 Eclipse 中为您提供。

它有很多东西你也可以从那里配置(如果你需要偏离约定,如果你不这样做,那么它已经准备好了):

一般来说,如果您使用 Maven,则不要手动更改 Eclipse 中的内容。切勿提交您的 .classpath/.project,而只需使用 Maven Eclipse 插件来生成这些文件(和/或更新它们)。这样你的项目就有一个配置源,MAVEN,而不是手动设置,你最终不得不告诉其他人使用并记住你自己等等。如果它是一个 Maven 项目:将其从源代码管理中签出,运行“mvn eclipse:eclipse”,它应该可以在 Eclipse 中工作,如果您的 POM 不需要工作的话。

I know this is an old question, but I stumbled on it and found it odd that none of the above answers mention the Maven Eclipse Plugin. Run mvn eclipse:eclipse and it will add src/main/resources and src/test/resources and such to the "Java Build Path" in Eclipse for you.

It has a bunch of stuff you can configure from there too (if you need to deviate from the conventions, if you don't then it's already ready to go):

In general, if you're using Maven, don't manually change stuff in Eclipse. Never commit your .classpath/.project and instead just use the Maven Eclipse Plugin to generate those files (and or update them). That way you have ONE configuration source for your project, MAVEN, and not manual settings that you end up having to tell others to use and remember yourself and so on. If it's a Maven project: check it out of source control, run "mvn eclipse:eclipse" and it should work in Eclipse, if it doesn't your POM needs work.

別甾虛僞 2024-09-09 20:24:41

如果您只是在测试时需要它,您可以通过文件系统加载它,这里的上下文对于两种情况应该是相同的:

new FileInputStream(new File("target/test-classes/your.file"));

不漂亮,但它可以工作

if you just need it at test time, you can load it via the file system, the context here should be the same for both cases:

new FileInputStream(new File("target/test-classes/your.file"));

not pretty, but it works

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