Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 700
Hans-Peter Störr Avatar asked May 17 '17 14:05

Hans-Peter Störr


People also ask

How do I set a breakpoint in IntelliJ?

Set breakpointsClick the gutter at the executable line of code where you want to set the breakpoint. Alternatively, place the caret at the line and press Ctrl+F8 .

How do I debug JUnit test cases in IntelliJ?

Debug failed tests If you don't know why a test fails, you can debug it. In the editor, click the gutter on the line where you want to set a breakpoint. There are different types of breakpoints that you can use depending on where you want to suspend the program. For more information, refer to Breakpoints.

How do I exclude test cases in IntelliJ?

On the Runner page, select Skip tests and click OK. IntelliJ IDEA de-activates the test goal under the Lifecycle node. The appropriate message notifying that tests are skipped is displayed in the Run tool window when you execute other goals.

Should JUnit tests throw exceptions?

The JUnit TestRunners will catch the thrown Exception regardless so you don't have to worry about your entire test suite bailing out if an Exception is thrown. This is the best answer. I'll add that I think the question here is one of style: catch-and-fail, or throw? Normally, best practice avoids "throws Exception".


2 Answers

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.

like image 102
Yamcha Avatar answered Nov 14 '22 21:11

Yamcha


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:

condition

like image 28
Morfic Avatar answered Nov 14 '22 21:11

Morfic