29

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

Eddified
  • 3,085
  • 8
  • 36
  • 47
  • I think the right way to say this, is how to use eclipse's post-mortem debugging within JUnit tests. – Eddified Feb 20 '13 at 23:14
  • 3
    Have you set an exception breakpoint for `java.lang.NullPointerException` or set a breakpoint on the line specified by the stacktrace? – beny23 Feb 20 '13 at 23:15
  • I found a duplicate question: http://stackoverflow.com/questions/13611711/junit-4-and-suspend-on-exception – Eddified Feb 20 '13 at 23:17
  • I did not know about exception breakpoints. I will look into it. But the point is I don't want to setup breakpoints at all. – Eddified Feb 20 '13 at 23:18
  • 2
    So are you just looking for someone to tell you that no, you can't have it stop like that for you automatically? – nitind Feb 21 '13 at 07:43
  • Yes, that's what I was looking for, a definitive "no". – Eddified Mar 16 '13 at 21:15

4 Answers4

9
  1. Open the Junit test case or Junit Test Suite you want to run.
  2. Place your Break Point.
  3. Right Click on the File and click on Debug As > Junit Test.
Dhruv Naik
  • 91
  • 1
  • 3
8

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).
Gus
  • 4,375
  • 5
  • 31
  • 50
3
  1. Double click set breakpoint

  2. Use Debug mode to run in Eclipse

Hover on that line of code, and then click step over or f6

Donald Wu
  • 698
  • 7
  • 20
0

You can see it on JUnit tab, if you double-click on the line that has a problem, you'll go to the location where the null-pointer exception happened.

java.lang.NullPointerException        <--First line of the error
at.com.myProject.MyClass.myMethod(MyClass.java:theLineOfMyCode) <-- the line that you should double-click

After you do this, you will be redirected to the line of the error.

Marco Blos
  • 933
  • 9
  • 22
  • 8
    I knew all this. The question is how to pause execution of the JVM at the point of the Exception, not how to find the location of the Exception. – Eddified Apr 28 '15 at 22:03