在 Eclipse 和 Maven 中使用 JUnit 的类路径问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2865533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 14:45:23  来源:igfitidea点击:

Classpath trouble using JUnit with both Eclipse and Maven

javaeclipsemaven-2junit

提问by Steve

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

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

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.

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

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.

当我用Maven构建这个项目,它需要的XML是在src/test/resources,所以它被复制到target/test-classes

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

我怎样才能让它们都只处理一个文件?

回答by Nate

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

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

The other issue is how getResourceAsStream("config.xml")works with packages. If the class that's calling this is in the com.mycompany.whateverpackage, then getResourceAsStreamis also expecting config.xml to be in the same path. However, this is the same path in the classpathnot 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 getResourceAsStreamload 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

另一个问题是如何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

回答by Ophidian

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.

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

回答by Charlie Collins

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:eclipseand it will add src/main/resourcesand src/test/resourcesand such to the "Java Build Path" in Eclipse for you.

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

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.

通常,如果您使用 Maven,请不要在 Eclipse 中手动更改内容。永远不要提交你的 .classpath/.project ,而只是使用 Maven Eclipse 插件来生成这些文件(和/或更新它们)。这样你的项目就有了一个配置源,MAVEN,而不是你最终不得不告诉其他人使用和记住你自己等等的手动设置。如果它是一个 Maven 项目:从源代码控制中检查它,运行“mvn eclipse:eclipse”,它应该在 Eclipse 中工作,如果它不需要你的 POM 需要工作。

回答by Sean Patrick Floyd

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

不漂亮,但它有效