javajacoco

Use jacoco to analyse a java program that would fail


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


Solution

  • 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.