Search code examples
javaeclipsejunitcode-coverageeclemma

Strange behavior of ECLEmma Code Coverage Tool in eclipse


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?


Solution

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