15

Is there a way to make Eclipse break on uncaught exceptions while in debug mode for jUnit? Eclipse breaks fine when executing main(). Is there a command-line switch I can use?

Thanks

User1
  • 39,458
  • 69
  • 187
  • 265

5 Answers5

8

From the debug perspective you can filter exactly which exceptions you are interested in.

In the Breakpoints view there is a "J!" button. This opens a window that allows you to choose which exceptions you want to break on.

If the problem only occurs when JUnit tests you need to make sure you are launching the tests in debug mode. The Rerun button in the JUnit will run in "normal" mode. To run the tests in debug you can right click on the file and select "Debug as -> JUnit Test" from the menu.

Aaron
  • 5,931
  • 4
  • 27
  • 31
  • 1
    Thanks for the insight. I am doing Debug as -> JUnit Test. It is stopping at breakpoints, but not at exceptions. It appears that JUnit swallows the exceptions (maybe with some sort of try/catch block). The code is a subclass of junit.framework.TestCase. The exception is caused by "int i=1/0;" for testing. – User1 Jul 15 '09 at 18:10
  • 1
    I can definately reproduce your problem. I never used to have this behaviour before. I found the following comment in the 3.5 release notes: Developers debugging applications on Sun's 1.6.0_14 virtual machine should be aware that breakpoints are unreliable (i.e. do not always suspend execution). The problem occurs on Windows and Linux platforms. At this point, it appears to be an issue with the VM rather than Eclipse. The workaround is to use the 1.6.0_13 virtual machine. (bug 279137). – Aaron Jul 15 '09 at 19:29
  • Did downgrading fix it on your side? I tried 1.6.0_13 and it didn't change anything. I'm pretty sure the downgrade is in effect, but not certain how to verify. – User1 Jul 16 '09 at 15:51
  • I just tried. It made no difference for me either. You can verify by looking at the debug launcher configuration for your test. Under the JRE tab it should tell you which jre you are using. – Aaron Jul 16 '09 at 19:51
  • I got this with testNG ... 1.6.0_16 – lisak Jun 18 '11 at 00:46
  • Execution of JUnit-Debug stops correctly in debugger mode after I defined e.g. "NullpointerException" explicitly in the list of Breakpoints (Window->Show View->Breakpoints), no matter of my settings in Window->Preferences->Java->Debug->"Suspend execution on uncaught exceptions". – Hartmut Pfarr Dec 09 '14 at 14:16
3

If you run in debug mode, this should be the default behaviour of later versions of Eclipse.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
3

As Thorbjørn Ravn Andersen said, it's the default behaviour since Eclipse Ganymede.
If it is not enabled, goto
Window->Preferences
Java->Debug
[X] Suspend execution on uncaught exceptions

It's very useful but it may be annoying, so try to figure out, what's the best option for you.

guerda
  • 23,388
  • 27
  • 97
  • 146
1

If you debug a single method in jUnit, the breakpoints start to work. If an entire class or package is debugged in jUnit, the debugger doesn't work.

User1
  • 39,458
  • 69
  • 187
  • 265
0

You have to select Run -> Debug from the menu. Eclipse will then stop on exceptions and breakpoints in your code.

Dan
  • 17,375
  • 3
  • 36
  • 39