1

there is a strange problem with the code coverage tool ECLEmma:

For the class MyFirstLogger I wrote the testclass MyFirstLoggerTest:

enter image description here

enter image description here

Please notice that I wrote in MyFirstLoggerTest a Testmethod where I expect to get a NullPointerException, just for the purpose to go through the else if-case in the setLogger-method in MyFirstLogger where logger is null and a NullPointerexception is thrown.

But why this branch is still yellow and not green? Is this a bug?

user3133542
  • 1,695
  • 4
  • 21
  • 42

1 Answers1

2

This behavior is a common one:

  • testNullableGetLogger is read because Emma (but and most other code coverage tool) mark a line as successful if it completed. Since it throw an exception (NullPointerException) the line (and the method!) could not be completed and are thus red.

  • else if (logger == null) is yellow because emma adds an implicit else case to the code. In your case this else case can never be entered because the if cases cover all states on logger. To get the line green replace it by else.

CoronA
  • 7,717
  • 2
  • 26
  • 53
  • Adds an implicit else ... where? to the "if" in line 16? EVen so, the *condition* in line 16 is still present (you said you checked byte code); why isn't it marked as executed? – Ira Baxter May 02 '15 at 09:47
  • [Yellow](http://www.eclemma.org/faq.html#usage08) means that it is partially executed. The hint in eclipse says '1 of 2 branches missed'. It is a common failure if the else branch is not entered, or - as here - not existing in source code. – CoronA May 02 '15 at 13:38
  • The described modification is not in the byte code of the class file. I did not inspect the runtime byte code, but it seams that each `if ... else if` without `else` gets a synthetic `else` or the missing `else` is at least reported in this way. – CoronA May 02 '15 at 13:46
  • No implicit "else" is added in the bytecode; each and every "==", "!=", etc. condition produces a conditional jump bytecode instruction. What happens is that the condition is redundant, since it will always evaluate to `true` *if* reached. Code coverage tools don't detect such redundancies, however, because it would require complex data flow analysis. – Rogério May 04 '15 at 21:36
  • @IraBaxter It's not marked as executed because the coverage tool (JaCoCo here, but JMockit Coverage too) does not perform data flow analysis. As far as I know, no coverage tool currently does that, including Clover. – Rogério May 04 '15 at 21:40
  • Thanks for clarification. This does mean, that Jacoco checks the traversal of all conditional targets and the remainder. Yet if the remainder is only visible in bytecode (as here) I would call it an implicit 'else'. As I now understand it is not added by Jacoco, it is already there. Do you agree? – CoronA May 05 '15 at 02:37