Hi,
I moving test code from Mockito version 1.10.19 to 2.12.0.
I am not using Mockito runner. @RunWith(SpringJUnit4ClassRunner.class)
My @Before method has the following
mockitoSession = Mockito.mockitoSession().initMocks(this).strictness(Strictness.STRICT_STUBS).startMocking();
My @After method has
mockitoSession.finishMocking();
In my test class I have
@Mock
private IStockService stockService;
so stockService is just a mock not a spy.
I wrote my test to have code 1A or 1B
1A)
when(stockService.computeProductExpirationDate(any(LocalDateTime.class), eq(WAREHOUSE_ID), eq(EXPIRATION_DELAY))).thenReturn(LocalDateTime.now());
1B)
doReturn(LocalDateTime.now()).when(stockService).computeProductExpirationDate(any(LocalDateTime.class), eq(WAREHOUSE_ID), eq(EXPIRATION_DELAY));
All constants are of type int.
For both approaches (1A or 1B) my test passes. Therefore, everything seems to be fine.
However, if I try to debug the test in Intelij IDEA I get the exception below for approach 1A ( not 1B ).
I am pressing F8 over all code lines in test and I get an exception when trying to execute 1A in debug mode.
If I remove the code in question (not A or B) then my test fails as the tested code needs the value returned by mock.
Exception is
////////////////////////////////
org.mockito.exceptions.misusing.WrongTypeOfReturnValue:
LocalDateTime cannot be returned by toString()
toString() should return String
If you're unsure why you're getting above error read on.
Due to the nature of the syntax above problem might occur because:
/////////////////////////////////////
My test is not multithreaded. My test is not using a spy.
There is something special about any matcher that causes this interesting behaviour. Even, in this test I can use when().thenReturn() approach even for the same mock and debug is ok. I did an experiment and commented out the other calls to when in this test (it makes the test fail), but it still does not help to avoid an exception above during debuging.
Normally, people have reverse issues. Tests passes when debugging and fails when executing. I have the reverse ;).
I am using
java version "1.8.0_151"
Java(TM) SE Runtime Environment (build 1.8.0_151-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.151-b12, mixed mode)
Intelij IDEA 2017.2.5
Hopefully my report will help you and not just waste your time. I am enjoying using your tool so I though I can raise this issue.
I saw the same behavior when IDEA tries to evaluate expression to show the value of variables. Seems it breaks mockito stubbing process.
To avoid this you have 2 options:
Enable auto expressions in Variables view@iceberk, you're correct.
@iceberk Could you please explain in more details? Option 2 works for me.
Thank You
for me option 2 doesn't work
Just had the same problem.
In addition to the above I also tried other things and got it to work. Going even further I realized the original change proposed was not needed after all using IntelliJ 2020.3 CE
So what remains was in IntelliJ 2020.3 CE:
File | Settings | Build, Execution, Deployment | Debugger | Data Views | Java - uncheck Enable 'toString()' object view
Most helpful comment
I saw the same behavior when IDEA tries to evaluate expression to show the value of variables. Seems it breaks mockito stubbing process.
To avoid this you have 2 options:
Enable auto expressions in Variables view