0

I'm on "Jacoco 0.8.0". I've got a switch statement with tests for all cases. Jacoco flags the throw and break in each case within the switch statement. The default case and break are unmarked. Why would it be flagging these?

I've looked around, and found posts like this that say previous versions had no filter for switch statements: Why is JaCoCo not covering my String switch statements?

But I'm running 0.8.0 which is after this fix, and the issue is slightly different.

MRE:

public class MyClass{

    public void myMethod(String let) {
           switch (let) {
               case "A":
                   throw new Exception();
                   break;

               default:
                   break;
           }
      }

}

Tests for each:

@Test(expected = Exception.class)
    public void testMyMethodA() {
        MyClass class = new MyClass();
       class.myMethod("A");
    }

@Test(expected = Exception.class)
    public void testMyMethodDefault() {
        MyClass class = new MyClass();
       class.myMethod("Z");
    }

Example of Flags:

public class MyClass{

    public void myMethod(String let) {
[YELLOW]  switch (let) {
               case "A":
[RED]          throw new Exception();
[RED]          break;

               default:
                   break;
           }
      }

}

Jacoco flags the throw statements and the breaks as untested. It flags the switch as partially tested. I expected these to be covered.

Tack
  • 121
  • 1
  • 12

1 Answers1

2

First of all your MRE

public class MyClass{

    public void myMethod(String let) {
           switch (let) {
               case "A":
                   throw new Exception();
                   break;

               default:
                   break;
           }
      }

}

can not be compiled by

javac --version
javac 11.0.3

because of following errors

javac MyClass.java
MyClass.java:7: error: unreachable statement
                   break;
                   ^
MyClass.java:6: error: unreported exception Exception; must be caught or declared to be thrown
                   throw new Exception();
                   ^
2 errors

If you'll have a look at JaCoCo homepage you can notice that that 0.8.0 is almost two years old and latest version is 0.8.4

And JaCoCo changelog in its usual location on page https://www.jacoco.org/jacoco/trunk/doc/changes.html contains

Release 0.8.2 (2018/08/21)

  • Part of bytecode that javac generates for switch statement on java.lang.String values with a small number cases is now correctly filtered out during generation of report (GitHub #730).

so that for corrected example

class Example {
    public void myMethod(String let) {
        switch (let) {
        case "A":
            throw new RuntimeException();
        default:
            break;
        }
    }

    public static void main(String[] args) {
        for (String s : new String[]{"A", "B"}) {
            try {
                new Example().myMethod(s);
            } catch (Exception ignore) {
            }
        }
    }
}

JaCoCo 0.8.4 produces following expected report

javac Example.java -d classes

java -javaagent:jacoco-0.8.4/lib/jacocoagent.jar -cp classes Example

java -jar jacoco-0.8.4/lib/jacococli.jar report \
    jacoco.exec \
    --classfiles classes \
    --sourcefiles . \
    --html report

report


As a rule of thumb and quoting JaCoCo release announcements

as always we recommend you to use latest released version

Godin
  • 9,801
  • 2
  • 39
  • 76