Java Code Coverage with EclEmma for Eclipse - Importance of Branch Coverage


Google+ Facebook Twitter LinkedIn Dzone Reddit Digg Blogger Hacker News Addthis

By Markus Sprunck; Revision: 1.2; Status: final; Last Content Change: Feb 13, 2013;

This short article describes the importance of branch coverage measurement during development and testing. It should motivate to use always the EclEmma Eclipse plug-in (see http://www.eclemma.org).

Don't get me wrong - not all code needs complete code coverage, e.g. generated getters/setters. But all code with business logic and/or utility classes like containers need full branch coverage. Independent from you policy you have to measure what you like to control.

Example Code

Some time ago, I had a discussion with a developer about test coverage and his statement was: "If we have 100% line coverage we tested everything!" So, I asked him to take a seat and we wrote together the following code:

// Foo.java
public class Foo {
       public String bee(final boolean init) {
              String s = null;
              if (init) { 
                    s = "bla+bla";
              }
              return s;
       }
}

If you look carefully at the class Foo, you will notice that there is a problem. The method will generate a null pointer exception if the parameter final boolean init of the method bee(final boolean init) gets the value false. This null pointer exception doesn't occurs for the first test method in the following JUnit test code, but the second method will create the error.

// FooTest.java
import junit.framework.Assert;
import org.junit.Test;

public class FooTest {
       @Test
       public void testBee() {
              Foo foo = new Foo();
              Assert.assertEquals(7, foo.bee(true).length());
              Assert.assertEquals("bla+bla", foo.bee(true));
       }

       @Test
       public void testBeeFail() {
              Foo foo = new Foo();
              Assert.assertEquals(7, foo.bee(false).length());
              Assert.assertEquals("bla+bla", foo.bee(false));
       }
}

This is a typical case, because the error happens depending on the use of the method. Maybe your code used the method always with true a parameter and everything works well. Another user of the class may use the parameter false and then get an exception.

Figure 1: Output from EclEmma


Because of this we need always an analysis of branch coverage and not only line coverage. A tool like EclEmma gives you an indicator that the branch coverage is not 100% and there is missing test code.

Recommendations
  • You are not ready with your test cases just because you reached 100% line coverage, quite often also branch coverage is important
  • Always use a tool code coverage tool like EclEmma for measuring the code coverage during development and when you write test cases

Change History

 Revision  Date  Author  Description
 1.0  Mar 20, 2012  Markus Sprunck   first version
 1.1  Aug 15, 2012  Markus Sprunck  improved layout for tablets
 1.2  Feb 13, 2013   Markus Sprunck  more detailed explanation why code coverage is important

Google+ Comments

You may press the +1 button to share and/or comment