0

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 is a normal Spring Boot JPA repository which here is being mocked with @Mock.

enter image description here

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

enter image description here

The entire error message is:

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

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?

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

halfer
  • 19,824
  • 17
  • 99
  • 186
ng.newbie
  • 2,807
  • 3
  • 23
  • 57
  • Could you prepare and provide a minimal reproducible project sample? https://stackoverflow.com/help/minimal-reproducible-example – Egor Klepikov Oct 19 '21 at 09:20

1 Answers1

4

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

Check your debugger settings

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.

See:

Don't call methods on mocked object during stubbing

See:

Lesiak
  • 22,088
  • 2
  • 41
  • 65