Search code examples
javaeclemma

eclemma showing red for else conditions in java which leads in getting low percentage of code coverage


In the below sample code eclemma highlighting

  • public class Test { and System.out.println("false"); as red
  • if (a<15) as yellow

I got code coverage percentage is 62%. Eclemma Report:

Missed Instructions Cov. 62% ,
Missed Branches Cov. 50% ,
Missed 2 , 
Cxty 3 ,
Missed 2 ,
Lines 7 ,
Missed 1,
Methods 2 ,
Missed 0 ,
Classes 1

Please someone explain me, how the code coverage is 62% only?

package sanityReady;
public class Test {
    public static void main(String[] args) {
        int a=10;
        if (a<15)
        {
            System.out.println("true");
        }
        else
        {
            System.out.println("false");
        }
    }
}

Solution

  • EclEmma usually tries to execute your Tests to calculate Code coverage. Your tests should cover all the conditions written in the normal java class.

    Usually when you write IF-ELSE together then EclEmma expects 2 tests to be written to test both conditions and if your test only tests either IF or ELSE then it marks that IF-ELSE yellow to indicate that it is partially tested!

    In above example, you have a=10 which is always less than 15 so your test is only testing IF part. Hence you are getting RED lines on else because it is not tested at all.

    You will need to refactor your code such that your tests should cover both IF and ELSE. Then only your code coverage will be 100 %.

    It seems that you are just testing how EclEmma works otherwise you should write proper Junit tests to test code coverage of your code.

    You can check this URL for learning Junits : http://www.tutorialspoint.com/junit/