Eddified
Eddified

Reputation: 3154

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

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

Upvotes: 29

Views: 74102

Answers (4)

user3458
user3458

Reputation:

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).

Upvotes: 8

Dhruv Naik
Dhruv Naik

Reputation: 121

  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.

Upvotes: 12

Donald Wu
Donald Wu

Reputation: 718

  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

Upvotes: 3

Marco Blos
Marco Blos

Reputation: 1046

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.

Upvotes: 0

Related Questions