为什么我的基于 Mockito 的单元测试无法在 Eclipse 中运行?

发布于 2024-10-06 12:01:23 字数 723 浏览 7 评论 0原文

我有一个 Maven 管理的项目,在其单元测试中使用 Mockito 模拟。我可以在 Maven 构建中运行所有测试,并且它们运行时没有错误(并且通过!)。但是,如果我右键单击单个测试函数,然后选择“Run As -> JUnit Test”,则会收到异常 java.lang.NoSuchMethodError: org.mockito.Mockito.doAnswer(Lorg/mockito/stubbing) /Answer;)Lorg/mockito/stubbing/Stubber;.当然,如果我查看源代码,就会发现“缺少”的方法,就像我所说的,测试是从命令行编译和运行的。

我能想到的最好情况是,Eclipse 是否试图通过为 JUnit 插件提供过时的 Mockito 工件(我在 Maven 依赖项中使用 1.8.5)来“帮助”我,类似于 Maven 插件如何让您坚持使用用于某些任务的 Maven 运行时的奇怪版本。

这是问题所在吗?难道是别的什么?我可以解决这个问题吗?

预计到达时间:显然这可能与已知问题有关。它很可能源于我的类路径中存在多个版本的 Mockito(感谢 Maven :-/ )。我的房子似乎井然有序 —— Eclipse 现在可以运行测试 —— 但不幸的是,这个错误已经困扰了我的 Hudson。我还必须找出如何从类路径中删除旧的 JAR。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

两仪 2024-10-13 12:01:23

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

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.

鲜肉鲜肉永远不皱 2024-10-13 12:01:23

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

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 ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文