为什么我的基于 Mockito 的单元测试无法在 Eclipse 中运行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4381759/
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
Why is my Mockito-based unit test unable to run in Eclipse?
提问by Coderer
I have a Maven-managed project that uses Mockito mocking in its unit tests. I can run all the tests within a Maven build, and they run without error (and pass!). However, if I right-click a single test function, and choose "Run As -> JUnit Test", I get an exception java.lang.NoSuchMethodError: org.mockito.Mockito.doAnswer(Lorg/mockito/stubbing/Answer;)Lorg/mockito/stubbing/Stubber;
. Of course, the "missing" method is there if I look at the sources, and like I said the tests compile and run from the command line.
我有一个 Maven 管理的项目,它在其单元测试中使用 Mockito 模拟。我可以在 Maven 构建中运行所有测试,并且它们运行时没有错误(并通过!)。但是,如果我右键单击单个测试函数,然后选择“运行方式 -> JUnit 测试”,则会出现异常java.lang.NoSuchMethodError: org.mockito.Mockito.doAnswer(Lorg/mockito/stubbing/Answer;)Lorg/mockito/stubbing/Stubber;
。当然,如果我查看源代码,就会发现“缺失”方法,就像我说的,测试是从命令行编译和运行的。
Best I can think of is if Eclipse is trying to "help" me by providing an outdated Mockito artifact (I'm using 1.8.5 in my Maven dependencies) for the JUnit plugin, akin to how the Maven plugin can stick you with an oddball version of the Maven runtime for certain tasks.
我能想到的最好的是 Eclipse 是否试图通过为 JUnit 插件提供一个过时的 Mockito 工件(我在我的 Maven 依赖项中使用 1.8.5)来“帮助”我,类似于 Maven 插件如何让你坚持用于某些任务的 Maven 运行时的奇怪版本。
Is this the problem? Is it something else? Can I fix this?
这是问题吗?是别的吗?我可以解决这个问题吗?
ETA: Apparently this may relate to a known issue. There's a good chance that it stems from having multiple versions of Mockito in my classpath (thanks, Maven :-/ ). I seem to have my house in order -- Eclipse can run the tests now -- but unfortunately the bug has bitten my Hudson. I have to track down how to remove the old JAR from the classpath there as well.
ETA:显然这可能与一个已知问题有关。很有可能它源于我的类路径中有多个版本的 Mockito(谢谢,Maven :-/)。我的房子似乎井井有条——Eclipse 现在可以运行测试了——但不幸的是这个错误已经咬住了我的 Hudson。我还必须追踪如何从那里的类路径中删除旧的 JAR。
回答by Bozho
Make sure the unit-test classpath has the correct mockito. You can check this from the run dialog. Btw, Eclipse does not ship with mockito, so perhaps you are having two versions of it. Take a look at your maven dependency graph and search for duplicates.
确保单元测试类路径具有正确的模拟。您可以从运行对话框中检查这一点。顺便说一句,Eclipse 不附带 mockito,所以也许你有它的两个版本。查看您的 Maven 依赖关系图并搜索重复项。
回答by Sanjeev Dhiman
I had the similar problem and I found that I had both "mockito-all 1.8.x" and "mockito-core 1.9.5" in my classpath. I was supposed to use only 1.9 but somehow eclipse was putting 1.8 before 1.9.5 in the classpath. I removed 1.8.x and it worked ;)
我遇到了类似的问题,我发现我的类路径中有“mockito-all 1.8.x”和“mockito-core 1.9.5”。我应该只使用 1.9 但不知何故 eclipse 将 1.8 放在 1.9.5 之前的类路径中。我删除了 1.8.x 并且它起作用了;)