简体   繁体   English

使用JUnit同时使用Eclipse和Maven的类路径问题

[英]Classpath trouble using JUnit with both Eclipse and Maven

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? 如何让它们只用一个文件?

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中,并将src / test / resources添加为Eclipse中的源文件夹。

The other issue is how getResourceAsStream("config.xml") works with packages. 另一个问题是getResourceAsStream("config.xml")如何与包一起使用。 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. 如果调用它的类在com.mycompany.whatever包中,则getResourceAsStream也期望config.xml位于同一路径中。 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 您可以将文件放在src / test / resources下的相同目录结构中 - src / test / resources / com / mycompany / whatever / config.xml - 或者您可以在路径中添加前导“/” - 这会使getResourceAsStream加载来自类路径基础的文件 - 所以如果你把它改成getResourceAsStream("/config.xml")你可以把文件放在src / test / resources / config.xml中

Try adding the src/test/resources/ directory as a source folder in your Eclipse project. 尝试将src/test/resources/目录添加为Eclipse项目中的源文件夹。 That way, it should be on the classpath when Eclipse tries to run your unit test. 这样,当Eclipse尝试运行您的单元测试时,它应该在类路径上。

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. 我知道这是一个老问题,但我偶然发现它并发现上述答案都没有提到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. 运行mvn eclipse:eclipse ,它会将src / main / resourcessrc / test / resources等添加到Eclipse中的“Java Build Path”中。

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. 通常,如果您使用的是Maven,请不要手动更改Eclipse中的内容。 Never commit your .classpath/.project and instead just use the Maven Eclipse Plugin to generate those files (and or update them). 永远不要提交.classpath / .project,而只是使用Maven Eclipse插件生成这些文件(和/或更新它们)。 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. 这样你就可以为你的项目配备一个配置源,MAVEN,而不是手动设置,你最终不得不告诉别人使用和记住你自己等等。 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项目:从源代码控制中检查它,运行“mvn eclipse:eclipse”它应该在Eclipse中工作,如果你的POM不需要工作的话。

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 不漂亮,但它的工作原理

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM