EclEmma fails to count method that throws an excep

2019-07-14 05:30发布

问题:

I am not getting to the 100% code covered and would like to. Unless I see the 100% green I wonder what I forget to test and go hunting only to find out silly things based on the tool and not my test are keeping me from it. Then later I forget and have to rinse/repeat.

While all paths are covered in testThrow because of the exception it is not counted as run.

Is there a way to re-write it so it is seen as covered towards that elusive 100% green.

public class Dummy {
    public void testThrow() throws Exception {
        throwException();       // This line is red and is seen as not covered.
    }

    private void throwException() throws Exception {
        throw new Exception();
    }
}

public class DummyTest() {
    @Test
    public void testThrow() throws Exception {
        new Dummy().testThrow();
    }
}

I added @Test(expected=Exception.class) but the line is still red.

I also tried:

public void testThrow() throws Exception {
    try {
        throwException();       // This line is STILL red
    }
    catch(Exception e) {
        throw e;                // This line becomes green (as expected)
    }
}                               // This line is now also red

回答1:

You can find the same thing in EclEmma documentation:

Why are JUnit4 test cases with expected exceptions shown as not covered?

JUnit4 test cases with expected exceptions are shown as not covered even though they were executed. The reason for this is that underlying JaCoCo code coverage library only considers code as executed when certain probes are executed. For successful test cases marked with @Test{expected=...} this is not the case.