仅在 Intellij 调试器中检测到 Mockito 未完成的存根异常 [英] Mockito unfinished stubbing exception detected ONLY in Intellij Debugger

查看:19
本文介绍了仅在 Intellij 调试器中检测到 Mockito 未完成的存根异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅当我尝试通过 IntelliJ 调试器调试测试时才会出现此问题.当我只是简单地运行测试时,它不会发生.

The problem only occurs when I am trying to debug a test via the IntelliJ debugger. It does not happen when I just simply run the test.

CustomerChoiceRepository 是一个普通的 Spring Boot JPA 存储库,这里使用 @Mock 进行模拟.

CustomerChoiceRepository is a normal Spring Boot JPA repository which here is being mocked with @Mock.

当此行在调试器中执行时,我在变量的监视部分收到以下错误:

When this line executes in the debugger I get the following error in the watch section of the variables:

整个错误信息是:

Method threw 'org.mockito.exceptions.misusing.UnfinishedStubbingException' exception. Cannot evaluate com.item.repository.jpa.CustomerChoiceRepository$MockitoMock$1318657964.toString()

这只是在 IntelliJ 调试器中检测到的,因此测试失败只有在我调试它时.

Again this is only is detected in the IntelliJ debugger, as a result the test fails only when I am debugging it.

所以我的问题是:这里发生了什么?

So my question is: what is happening here?

这是一个错误吗?这是因为我对 Mockito 的内部结构不是特别了解而无法理解的吗?

Is this a bug ? Is this something that I fail to understand because I do not know the internals of Mockito particularly well ?

推荐答案

你的 IntelliJ 在 stubbing 期间在 mock 上调用 toString,导致异常.

Your IntelliJ is calling toString on the mock during stubbing, which causes the exception.

检查您的调试器设置

默认情况下,IntelliJ 在调试器窗口中的对象上调用 toString,但仅在覆盖该方法的对象上调用.此行为是可自定义的,目前在 Preferences ->;构建工具->调试器->数据视图 ->爪哇->启用 'toString' 对象视图,但 IDE 版本之间的设置可能有所不同.

By default, IntelliJ calls toString on objects in debugger window, but only on objects that override that method. This behaviour is customizable, currently in Preferences -> Build Tools -> Debugger -> Data Views -> Java -> Enable 'toString' object view, but the settings mey differ between IDE versions.

见:

在存根期间不要调用模拟对象的方法

见:

这篇关于仅在 Intellij 调试器中检测到 Mockito 未完成的存根异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆