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 CodeSome 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;
}
}
// 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));
}
}
Recommendations
Change History
|
