问题
When I have a JUnit test, I'd like the debugger to stop right at the point where any exception in the tested code is thrown that lets the test fail, in order to inspect what's wrong. Is there a possibility to configure IntelliJ's debugger to do that, and not break on other exceptions?
If I set an exception breakpoint on any uncaught exception, this doesn't work since the exception is caught by JUnit. If I try to have the breakpoint break also on caught exceptions with catch class org.junit., that doesn't work either, since the exception is caught and wrapped by Javas reflection mechanisms before it reaches JUnit. So I'm a bit at loss here - Eclipse just stops at the original exception.
CLARIFICATION: I am talking about exceptions in the code I test or code called from there, not about assertion failures in the tests. For example, consider these tests:
@Test
public void thisShouldBreak() {
int i = 25 - 25;
int j = 17 / i;
}
private void neverBreakHereSinceThisIsCaught() {
int i = 14 - 14;
int j = 29 / i;
}
@Test
public void thisShouldNotBreak() {
try {
neverBreakHereSinceThisIsCaught();
} catch (ArithmeticException e) {
// ignored or handled
}
}
@Test
public void thisShouldNotBreakEither() {
try {
getClass().getDeclaredMethod("neverBreakHereSinceThisIsCaught").invoke(this);
} catch (Exception e) {
// ignored or handled
}
}
I want IntelliJ to stop when executing test thisShouldBreak
at the place where the ArithmeticException is thrown, so that I can inspect the value of i that caused the exception. However, I do not want IntelliJ to stop in neverBreakHereSinceThisIsCaught
since the exception thrown there doesn't reach JUnit. I tried unsuccessfully:
- an exception breakpoint on caught exceptions breaks in neverBreakHereSinceThisIsCaught, too, and loads of other places.
- an exception breakpoint only on uncaught exception is never hit at all, since JUnit catches and wraps those exceptions.
- a catch class filter
org.junit.*` breaks in lots of internal places of JUnit end Java reflection calls by JUnit, too.
回答1:
This is what I did:
What you basically want to do is tell Intellij to stop on any exception (Caught & Uncaught) where the catch class filter is junit, so that caught exceptions only by junit cause the debugger the stop.
Here's my Intellij settings:
You might run into a similar issue when you use a app server like Tomcat, which catches exception. The same way, you would filter the catch class by Tomcat packages.
回答2:
You could add a filter to check if the stack trace contains your package. It will probably make the execution slower, but it will not stop for JUnit initialisation exceptions that don't prevent test execution anyway, and it will stop only if the calls involve some of your classes. Something along the lines of:
回答3:
How I deal with this:
- Set Java Exception Breakpoint to java.lang.AssertionError
Debug a test that fails, it will break inside Assertion.java
Look at the debugger and navigate to the test you want to debug as shown here:
You can now check variables, evaluate expressions, etc in your test
回答4:
If you only need exceptions that are caught in JUnit, you can use "catch class filters" in exception breakpoint and specify a class inside JUnit where they are catched.
回答5:
After fiddling around for a while, the following seems to work best:
"Any exception" Breakpoint on caught and uncaught exceptions with a catch class filter org.junit.runners.*
and a class filter -org.junit.*
to get rid of caught exceptions that are thrown by the JUnit mechanics itself.
A unconditional breakpoint on caught and uncaught AssertionError seems to be a good idea, too, to inspect the data in the local variables in the test itself when an assertion fails.
来源:https://stackoverflow.com/questions/44028088/can-intellij-idea-be-configured-to-break-on-exceptions-in-junit-tests-that-lets