无法在 Eclipse 中调试 Mockito / JUnit 代码,仅使用 JUnit 即可正常工作

发布于 2024-09-07 22:32:25 字数 165 浏览 10 评论 0原文

我的 JUnit 测试运行得很好。将 Mockito 添加到我的构建中,并尝试在 JUnit 测试中放置一个断点,该测试使用 Mockito 来模拟一些公共方法。当我尝试在类上运行调试器时,出现错误“由于缺少行号属性,无法在 XXX 中安装断点。修改编译器选项以生成行号属性。”我检查了我的编译器,并选择了生成行号。

I've got JUnit tests that run just fine. Added Mockito to my build and I try to put a breakpoint in my JUnit test that uses Mockito to mock out some of the public methods. When I try to run the debugger on the class, I get the error "unable to install breakpoint in XXX due to missing line number attributes. Modify compiler options to generate line number attributes." I checked my compiler and I generate line numbers is selected.

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

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

发布评论

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

评论(6

疯到世界奔溃 2024-09-14 22:32:25

您看到的异常是由于尝试调试由 mock() 函数创建的动态生成的空模拟方法引起的。从您的问题来看,您似乎实际上希望使用部分模拟而不是完整模拟,其中仅模拟某些方法,而其余调用则委托给真正的实现。

要创建部分模拟,您应该使用 spy() 方法而不是 mock() 方法。
所以,使用

MyClass myMock = spy(new MyClass());

而不是

 MyClass myMock = mock(MyClass.class);

The exception you're seeing is caused by attempting to debug dynamically generated empty mock methods created by the mock() function. From your question, it looks like you actually want to be using partial mocks instead of full mocks, where only some methods are mocked and the remaining calls are delegated to the real implementation.

To create partial mocks, you should be using the spy() method instead of the mock() method.
So, use

MyClass myMock = spy(new MyClass());

instead of

 MyClass myMock = mock(MyClass.class);
驱逐舰岛风号 2024-09-14 22:32:25

尝试删除并重新添加断点,可能只是当前断点引用了旧版本的类。就这样!

也许Mockito 群组中的这篇帖子可以帮助您。

Try by removing and re-adding your breakpoints, it might just be that a current breakpoint references an old version of a class. Just that!

Maybe this post in the Mockito group can help you.

掩于岁月 2024-09-14 22:32:25

我有同样的消息(Eclipse Luna)。

尽管有大量错误消息,如果调试器到达断点,调试仍然有效。您只需在所有消息上单击“确定”,或禁用这些消息。

我认为问题源于在扩展类上放置断点(mockito 可能是动态扩展模拟类)并且 Eclipse 无法追踪源代码。

I have the same messages (Eclipse Luna).

In spite of the large number of error messages, the debugging is still working if the debugger hits your breakpoint. You just have to click 'ok' on all of them, or disable these messages.

I think the problem originates from placing breakpoints on an extended class (mockito probably is dynamically extending the mocked classes) and Eclipse being unable to trace down the source code.

九公里浅绿 2024-09-14 22:32:25

如果仅在使用 Mockito 时发生,那么可能是因为 Mockito 是在没有调试器支持的情况下编译的?

另外,请检查测试类的编译器设置是否与常规代码相同。

If it only happens when you use Mockito, then maybe it's because Mockito was compiled without debugger support?

Also, check that you have the same compiler settings for your test classes as for your regular code.

空‖城人不在 2024-09-14 22:32:25

当您在模拟方法中放置断点时会发生

Happens when you put a breakpoint in a mocked method

情绪操控生活 2024-09-14 22:32:25

除了这个问题很老之外,我今天也遇到了同样的问题,解决方案很简单,但花了一些时间才弄清楚。
所以这可能对在这里绊倒的人有帮助。

我刚刚设置了一些旧的断点,其中一个断点指向一些更改的代码,因此记录的断点位置不再合适。

我的建议是尝试删除所有有问题的断点并在当前代码上重新安装它们。在干净的构建之后,只是为了确保指向最新的二进制文件。 :)

besides this question being old I had same problem today and the solution was quite simple, but took some time to figure it out.
So this may be of help to who stumbles here.

I just had some old breakpoint set and one of them was pointing to some changed code, so that the recorded breakpoint position was not good anymore.

My advice is to try to remove all the offending breakpoints and reinstall them on the current code. After a clean build, just to be sure to point to the latest binary. :)

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