mockito: 'this' is not available - when enabling mock-maker-inline

When we enable mock-maker-inline in our project and debug a class with @Spy annotated on it IntelliJ reports that 'this' is not available: image

When we unset mock-maker-inline then things work as expected and we can inspect the class variables: image

It appears to be the same as this report: https://stackoverflow.com/questions/62661996/unable-to-debug-junit-test-anywhere

Is this expected Mockito behaviour?

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 1
  • Comments: 33 (19 by maintainers)

Most upvoted comments

Same issue with Mockito-inline 3.8.0 and java 15. This makes tests very difficult to debug, because it makes impossible to debug the code step by step

It should fix both. A new release is underway in this minute.

@TimvdLippe and @raphw - I missed that part in the discussion so went back and added a few more lines to the method in the spied class and I noticed a few things. If I put the break point in the method that I’m calling it never gets tripped. When I put it in the test and step into it I’m getting the error on line one (expected) and then my next step always jumps to the end of the method - I do get ‘this’ at that point though.

I just tested it myself in IntelliJ, and it does now work as expected. There is one caveat, however. If you set the breakpoint in the first line of the method, it will:

a) not contain the this reference if stopped in the first line. b) trigger the breakpoint for mocked method calls.

This is something we can hardly avoid, unfortunately. But we should certainly document it in the Mockito class.

@jwb441 Could you double-check that you aren’t accidentally using an older version of ByteBuddy via a different transitive dependency? I can retest this issue tomorrow.

@iamkarlson We first need a new release of ByteBuddy. Then we can roll that into Mockito and release Mockito as well. We can do that relatively quickly, so it’s mostly blocked on the ByteBuddy release.

I am afraid we are the wrong place to fix this. Byte Buddy needs to shift the local variable array to add Mockito’s logic. For reasons ™, this requires to move the this variable further to the left. It is however still there:

  LocalVariableTable:
    Start  Length  Slot  Name   Signature
       76       4     2  this   Lfoo/Sample;

IntelliJ could certainly pick this up and make the variable available in the debugger. Normally this is put at slot 0 but the JVM does not require this at all. I think IntelliJ falsely assumes this. If I rename the variable to this2, it is even displayed but that would be inconsistent for other parsing processors. This needs to be fixed in IntelliJ.

Rafael, does this mean we need to generate some more bytecode to make sure that this references are working as intended with the inline mockmaker?

Thanks for filing the issue. Could you please provide us a minimal reproduction case that we can clone/download to debug the issue? Thanks in advance!