在 Eclipse 和 Maven 中使用 JUnit 时出现类路径问题
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将 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.xmlPlace 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 thecom.mycompany.whatever
package, thengetResourceAsStream
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 makesgetResourceAsStream
load the file from the base of the classpath - so if you change it togetResourceAsStream("/config.xml")
you can just put the file in src/test/resources/config.xml尝试将 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.我知道这是一个老问题,但我偶然发现了它,并发现上面的答案都没有提到 Maven Eclipse 插件,这很奇怪。运行
mvn eclipse:eclipse
,它将添加src/main/resources和src/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.
如果您只是在测试时需要它,您可以通过文件系统加载它,这里的上下文对于两种情况应该是相同的:
不漂亮,但它可以工作
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:
not pretty, but it works