More‎ > ‎

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


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

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:

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.

import junit.framework.Assert;
import org.junit.Test;

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

       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.


  • 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

Sponsored Link