Can Intellij-IDEA be configured to break on exceptions in JUnit tests that lets the test fail?

Hans-Peter Störr :

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 filterorg.junit.*` breaks in lots of internal places of JUnit end Java reflection calls by JUnit, too.

Yamcha :

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: Intellij breakpoint 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.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=463451&siteId=1