Untestable Condition Coverage in Java with || or && operator

Hi Sonar-Team,
We have a problem with the condition coverage of our java projects when scanning them. The code is something like

AnyEnum object = anyVariable == null || NULL_STATE.equals(anyVariable) ? null : ANY_VALUE;
// or
NumberObject object = anyNullableNumber == null && anyNullableNumber >= 10 ? 1 : 0;

Beside the horrible abstract examples sonar shows 4 conditions which we have to cover. The problem is, that it is not possible for a variable to be null or any value at the same time. Therefore it is not testable. This is a feature of java not to check the second condition of the || or the && operator when the first is true (for ||) or false (for &&). We use this mechanism e.g. for nullchecking in the same line.
Sonar is not able to get, that in these cases there are just 3 coniditions which we are able to cover and therefore the pipeline fails.

Ofcourse we could decrease our condition coverage or change our code in order to fit to sonar, but as we do not change our code for tests we do not want to change for sonar either.

We would be very pleased to receive feedback
Greetings

Laksuh


  • ALM used: GitLab
  • CI system used: GitLab CI
  • Scanner command used when applicable: mvn sonar:sonar -Dsonar.host.url=MASKED -Dsonar.organization=MASKED -Dsonar.projectKey=MASKED -Dsonar.qualitygate.wait=true
  • Languages of the repository: Java

Hi Laksuh,

We don’t actually evaluate what’s covered or not covered. You feed coverage reports in to analysis and we use them verbatim. This is something you would need to take up with the maintainers of your coverage engine.

 
Ann

Hi Ann,
thanks for your Reply.
But isn’t it sonar which prevents me from a green pipeline? I can’t even mark the condition coverage as “false positive” as in the code smell section or something similar. I get the point that it is non of your business to calculate the coverage, don’t get me wrong please, but as end user I have “trouble” with sonar.
Is there anything planned in order to work around this issue (like e.g. a “ignore this case” option)?

Thank you very much and
Greetings

Laksuh

Hi Laksuh,

We’ve definitely been talking about this, although I can’t give you any timelines

 
Ann

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.