1

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.

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.

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.

4

2 回答 2

7

确保单元测试类路径具有正确的模拟。您可以从运行对话框中检查这一点。顺便说一句,Eclipse 不附带 mockito,所以也许你有它的两个版本。查看您的 Maven 依赖关系图并搜索重复项。

于 2010-12-07T21:29:08.403 回答
1

我遇到了类似的问题,我发现我的类路径中有“mockito-all 1.8.x”和“mockito-core 1.9.5”。我应该只使用 1.9,但不知何故 eclipse 在类路径中将 1.8 放在 1.9.5 之前。我删除了 1.8.x 并且它起作用了;)

于 2015-07-14T21:11:33.573 回答