How to Debug JUnit tests similar to a regular Java program within Eclipse

debugging, eclipse, java, junit

Solution

Eclipse stops when the exception that's thrown is uncaught and would bump you out of main(). However, when you run with JUnit framework, all exceptions are caught by JUnit, so Eclipse does not stop.

Two solutions come to mind:

- set exception breakpoint to stop when `NullPointerException` is thrown

- use the exception's stack trace reported by JUnit and set the breakpoint on the line that throws the exception (that's the one I prefer).

Problem

In eclipse, if I run a Java program in debug mode with no breakpoints, and if the JVM hits a NullPointerException, Eclipse will kindly highlight the offending line of code, and show the local variables, etc. Execution is paused. I can evaluate code using the Display tab, and so on. But, if I run a JUnit test in debug mode, and the jvm hits a NullPointerException, the jvm does not pause, and I don't have a chance to see the local variables. Is it possible to run JUnit tests so that the JVM will pause automatically when I hit a NullPointerException, without using breakpoints? Edit: Using JUnit 4 & Juno

Original source