1

I have a java program and I'm using jacoco to do coverage analysis. Now I encounter a problem that jacoco treat the line throwing out an exception as unexecuted. For example in the following program:

public static Number createNumber(final String str) throws NumberFormatException {
        if (str == null) {
            return null;
        }
        if (StringUtils.isBlank(str)) {
            throw new NumberFormatException("A blank string is not a valid number");
        }
        final String[] hex_prefixes = {"0x", "0X", "-0x", "-0X", "#", "-#"};
        int pfxLen = 0;
        for(final String pfx : hex_prefixes) {
            if (str.startsWith(pfx)) {
                pfxLen += pfx.length();
                break;
            }
        }
        if (pfxLen > 0) { // we have a hex number
            final int hexDigits = str.length() - pfxLen;
            if (hexDigits > 16) {
                return createBigInteger(str);
            }
            if (hexDigits > 8) {
                return createLong(str);
            }
            return createInteger(str); // this line would throw out an exception
        }
        ...
}

createInteger throw out an exception. So the report of jacoco looks like this: jacoco_report_of_a_failed_program

What I want is like this: expected_jacoco_report_of_a_failed_program

I have tried to surround the code with try/catch like following but the result unchanged.

try {
    NumberUtils.createNumber("0x80000000"));
} catch (Exception e) {
    System.out.println("Error: " + e);
}

Edit: I'm using java11 and jacoco 0.8.12

3
  • This looks like a bug in JaCoCo, but I've never seen such a bug in my own code. I had similar code, ending with a method call that throws an exception, and it showed coverage as expected. Maybe add your versions of JaCoCo and Java to your question. Commented Dec 10, 2024 at 10:23
  • @k314159 Thanks a lot. Could you show me your JaCoCo results and related code snippets? I want to compare them and try to figure out why they behave differently. Commented Dec 11, 2024 at 3:35
  • You're right, I've reproduced your issue! JaCoCo doesn't show those lines as covered, even though they clearly were. I am used to seeing coverage reports by running tests directly from IntelliJ IDEA. In the IDE, those lines are shown as covered. But when running JaCoCo from the Gradle command line, it shows just like in your screenshot. Commented Dec 11, 2024 at 14:36

1 Answer 1

0

This is a known issue. See https://github.com/jacoco/eclemma/issues/61

This issue is marked as "won't fix". It is a limitation caused by how JaCoCo works, using probes.

Sign up to request clarification or add additional context in comments.

1 Comment

what a pity and thx a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.