vscode-java-debug icon indicating copy to clipboard operation
vscode-java-debug copied to clipboard

Breakpoints not set when using org.mockito:mockito-inline

Open sfrank-mc opened this issue 4 years ago • 6 comments

When using org.mockito:mockito-inline in unit tests, trying to debug the tests is harder because upon starting the debug breakpoint will have to be created after all mocks/spies are created. In addition all live context becomes invisible.

Environment
  • Operating System: MacOS
  • JDK version: openjdk version "11.0.7" 2020-04-14 OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.7+10) OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.7+10, mixed mode)
  • Visual Studio Code version: 1.53.2 622cb03f7e070a9670c94bae1a45d78d7181fbd4 x64
  • Java extension version: v0.12.1
  • Java Debugger extension version: v0.31.0
Steps To Reproduce
  1. run git clone https://github.com/sfrank-mc/vscode-java-debug-bug (or go through the code and replicate locally)
  2. open the workspace inside the vscode-java-debug-bug with vscode
  3. open file at src/main/java/vscode/java/debug/bug/App.java and place a breakpoint at line 8
  4. open file at src/test/java/vscode/java/debug/bug/AppTest.java and debug the test called testAppHasAGreeting
  5. notice test will pass without breakpoint being hit.

Workaround and second issue:

  1. open file at src/test/java/vscode/java/debug/bug/AppTest.java and set a breakpoint at line 15
  2. debug the test called testAppHasAGreeting
  3. breakpoint at line 15 is hit. don't continue.
  4. open file at src/main/java/vscode/java/debug/bug/App.java
  5. remove the breakpoint at line 8
  6. re-create the breakpoint at line 8
  7. continue running the debug session (F5)
  8. notice breakpoint is hit.
  9. notice there is no live context info: Screenshot 2021-03-04 at 09 56 03

Control check:

  1. open file at build.gradle
  2. comment line 30
  3. uncomment line 31
  4. perform steps 3 and 4.
  5. notice breakpoint is hit.
Current Result

Debug breakpoints broken in every run No debug info even if breakpoint is re-created

Expected Result

Debug breakpoints should be maintained between runs Debug info should be available

sfrank-mc avatar Mar 04 '21 10:03 sfrank-mc

For the screenshot you shared, actually that stack frame doesn't contain any local variables. The fix for the debugger is just to ignore the exception.

But i have one question for your sample: What's the difference between the statement App classUnderTest = spy(new App()); and App app = new App();? I don't have the knowledge of the library mockito-inline you used.

testforstephen avatar Mar 08 '21 07:03 testforstephen

Thanks for your reply @testforstephen,

Regarding the stack frame in the image - it should it think at least include this as this is an instance method - am I wrong? This is the picture of same breakpoint when I use mockito-core instead of mockito-inline: image

Regarding your question:

spy() will wrap an object interface so that it could be used for test units.

Mockito is a supporting framework that helps perform unit testing. It allows you to mock and/or spy on objects and then verify/assert certain facts that happened to them while keeping them from performing actions that may affect or be affected by the environment thus isolating the "unit" - allowing you to "unit test". Mockito-Inline is part of the framework that allows additional features including mocking/spying on static methods etc...

You can find more info here:

  • https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/Spy.html
  • https://www.baeldung.com/mockito-spy
  • https://javadoc.io/static/org.mockito/mockito-core/3.8.0/org/mockito/Mockito.html#mockito-inline

Hope that helps.

sfrank-mc avatar Mar 08 '21 08:03 sfrank-mc

Regarding the stack frame in the image - it should it think at least include this as this is an instance method - am I wrong?

It seems no this context for your sample case. The error you saw in VARIABLES view is actually from resolving this object from the stack frame. And I tried other IDEs, no variables as well.

And thank you for providing the information about the spy usage, i will take a look to see what happens at the background.

testforstephen avatar Mar 08 '21 09:03 testforstephen

Thanks.... My bad from not being clear... I was referring to the this...

As you can see in the two picture:

  1. When debugging using mockito-inline the this is not displayed - showing the error line you mentioned
  2. When debugging using mockito-core the this is displayed correctly

As these are both from the same file and line in the code I would think they should have the same behaviour. Am I wrong?

sfrank-mc avatar Mar 08 '21 10:03 sfrank-mc

I got your point, it should have some runtime differences between these two cases. Our debugger just reflects what the application JVM gives us and doesn't do additional processing.

testforstephen avatar Mar 08 '21 13:03 testforstephen

Hi @sfrank-mc, I'm an AI Support assistant here to help with your issue. While the team reviews your request, I wanted to provide some possible tips and documentation that might help you in the meantime.

  • Workaround: switch back to the regular mockito-core mock maker (comment out the mockito-inline dependency in your build and use org.mockito:mockito-core instead). This restores normal debugging behavior.
  • Known issue in the Java Debugger with inline/instrumented code, see: • inline breakpoint will not work on some conditions (#1414) – describes breakpoints silently failing when classes are instrumented (e.g. by inline mock maker).
Other references with low confidence
  • Support update the invalid breakpoint into next valid line. Discussion about invalid breakpoint handling in VS Code Debugger. No direct fix for inline/instrumented code. https://github.com/microsoft/vscode-java-debug/issues/90#issuecomment-372213181

The team will respond to your issue shortly. I hope these suggestions are helpful in the meantime. If this comment helped you, please give it a 👍. If the suggestion was not helpful or incorrect, please give it a 👎. Your feedback helps us improve!

github-actions[bot] avatar Nov 11 '25 08:11 github-actions[bot]