从命令行安装 Maven 时测试失败,但从 Eclipse UI 测试通过
我在我的Java项目中使用Maven,IDE是Eclipse。我的项目中只有很少的测试用例,当我从 eclipse 进行 Maven 安装时,我的测试用例会通过,但是当我从命令行进行 Maven 安装时,我的所有测试用例都会失败。我的项目有以下目录结构: 源代码/ 主要的/ 爪哇/ 资源/ 测试/ 爪哇/ resources/
另外,对于我的测试用例,我必须使用 main/resources 中的一些配置。我怀疑从命令行(mvn clean install)运行测试用例时,它没有在 main/resources 中查找资源,因此错误也是如此。
谁能告诉我如何让maven 在 main/resources 中为我的测试用例查找配置?另外,如果您怀疑该错误是其他原因,请发表评论。
谢谢。
I am using Maven in my Java project and the IDE is Eclipse. I have few test cases in my project, and when I am doing a Maven Install from eclipse then my test cases are passing, but when I am doing the Maven install from the command line, then all my test cases are failing. I have the following directory structure for my project:
src/
main/
java/
resources/
test/
java/
resources/
Also, for my test cases, I have to use few configs from main/resources. I suspect that while running test cases from the command line (mvn clean install), it is not looking for the resources in main/resources and so is the error.
Can anyone please tell that how can I ask maven to look for the configs in main/resources also for my test cases? Also, if you suspect that the error is something else then please comment.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您的测试使用 maven 命令行失败,请确保您的测试存在问题。为此,您不能依赖 eclipse,或者更确切地说 m2Eclipse,因为 m2eclipse 无法为您提供正确的类路径。
例如,在 Eclipse 中,您可以从 src\main\java 中的类引用 src\test\java 中的类,不会出现任何编译错误。当然,在maven(或者像intelliJ这样具有不错的maven集成的IDE)中编译将会失败,这是理所当然的。在 eclipse 中运行测试很好(比 maven 命令行更快/更容易),但您必须执行的实际测试(例如在提交到 svn 之前)是使用 maven 命令行进行全新安装。
更新
解决您的问题:当 maven (surfire) 运行测试时,src/main/resources 位于类路径中
If your tests are failing using maven command line then rest asure there is a problem with your test. You can't rely on eclipse, or rather m2Eclipse, for this because m2eclipse is not able to provide you with a correct classpath.
For instance, in eclipse you can refer from a class in src\main\java to a class in src\test\java, you won't get any compilation error. Of course, in maven (or an IDE with a decent maven integration like intelliJ) compilation will fail, as it should. Running tests in eclipse is fine (quicker/easier than maven command line) but the actual test you must perform (before committing to svn for instance) is to do a clean install with maven command line.
UPDATE
To address your question: src/main/resources is in the classpath when maven (surfire) runs the tests
我遇到了同样的问题,在带有 JUnit 的 Eclipse 中测试一切正常,但当我在控制台中执行
mvn test
时,在 Maven 中测试失败。我通过添加到我的
pom.xml
中解决了这个问题。
I had the same problem, tests are all ok in Eclipse with JUnit, but fail in maven when I do
mvn test
in the console.I solved this issue adding
to my
pom.xml
.我的问题略有不同,但症状相同。
我在 src/main/resources 和 src/test/resources 中有两个文件名相同的文件。
src/test/resources
中的文件不正确,但正在覆盖src/main/resources
中的新文件。我的解决方案是简单地从 src/test/resources 中删除该文件。
I had a slightly different issue with same symptoms.
I had two files with same filename in
src/main/resources
andsrc/test/resources
. The file fromsrc/test/resources
was incorrect, but was overwriting the newer file fromsrc/main/resources
.The solution for me was to simply delete the file from
src/test/resources
.我通过禁用我的防火墙 - Comodo - 解决了这个问题 - 尽管我告诉它不要将 Surefire jar 沙箱化,但该死的东西仍然阻止了它。禁用沙箱,一切都很好。
I solved this by disabling my firewall - Comodo - even though I told it not to sandbox surefire jar, damn thing was still blocking it. Disabled sandbox and everything was fine.