Break on Exception in Eclipse using jUnit

2019-02-06 08:37发布

问题:

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

回答1:

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.



回答2:

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



回答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.



回答4:

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.



回答5:

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