This seems to be a problem with the JaCoCo Sonar code coverage component. JaCoCo works with compiled bytecode, not a Java source, and the Java compiler can generate code that is not directly related to the source source.
Looking at the docs for JaCoCo, there is a section that reads (highlighted by me):
In some situations, it is not obvious why individual lines have a highlight or a certain color. The reason is that the JaCoCo base code coverage library only works with Java class files. In some cases, the Java compiler generates extra byte code for a particular line of source code . Such situations may be filtered out in future versions of JaCoCo / EclEmma.
By following the link in the passage, you will be taken to the FilteringOptions page on the Jacoco GH website, and it mentions a number of ways that the JDK can create code that will trigger these “false” warnings about code coverage.
However, this is not what is played here (or not quite).
As already mentioned, JaCoCo runs on Java bytecode, so any code that is generated by the compiler that is not directly bound to the source will be considered for coverage.
In my particular case, I had an assert , which in the source is a branch at the point where the statement occurs, but also at the "global" level. If you look at the bytecode for the Foo class defined above (do a javap -c Foo ), you will see:
Compiled from "Foo.java" public class Foo extends java.lang.Object{ static final boolean $assertionsDisabled; Foo(java.lang.String); Code: 0: aload_0 1: invokespecial
Note 7 line, which is a conditional branch depending on whether statements are included. Thus, if you have a class with a simple Java assert in it, you will have this branch somewhere in the bytecode, and this is what leads to a warning about the coverage of "N / 2 branches" in the class declaration, where N is either 0 or 1, depending on when the class was ever executed as a result of the test (1) or not (0).
Edit: note that this is also mentioned in https://sourceforge.net/apps/trac/eclemma/wiki/FilteringOptions :
Blocks that throw AssertionErrors - the entire block should be ignored if the condition (if! Assertion throw new AssertionError)