Friday 20 March 2015

Eclipse: EclEmma code coverage tool


In this post i am going to explain about code coverage tool “EclEmma”. EclEmma is a free Java code coverage tool for Eclipse, available under the Eclipse Public License.

Install EclEmma on Eclipse
1. From your Eclipse menu select Help → Install New Software.

2. In the Install dialogue enter http://update.eclemma.org/ at the Work with field.

Below steps explain step by step procedure on how to use EclEmma.

Step 1: Create new maven project “eclemma_tutorial”. If you don't install maven plug-in, install using instructions here.

Step 2: I am going to use junit for testing my Application, So add junit dependency to pom.xml file.

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>eclemma_tutorial</groupId>
 <artifactId>eclemma_tutorial</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <dependencies>
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>4.11</version>
  </dependency>
 </dependencies>
</project>


Step 3: Create Main class, This class contains one method “getDay”, which returns day of the week.

I am going to create 2 classes.
    Main.java : Application class.
    TestMain.java : Test class to test Main.java

Project structure looks like below.
 
Step 3: Create Main class, This class contains one method “getDay”, which returns day of the week.
package eclemma_tutorial;

public class Main {
  public static String getDay(int day) {
    String today = null;
    switch (day) {
      case 1:
        today = "Monday";
        System.out.println("Monday");
        break;
      case 2:
        today = "Tuesday";
        System.out.println("Tuesday");
        break;
      case 3:
        today = "Wednesday";
        System.out.println("Wednesday");
        break;
      case 4:
        today = "Thursday";
        System.out.println("Thursday");
        break;
      case 5:
        today = "Friday";
        System.out.println("Friday");
        break;
      case 6:
        today = "Saturday";
        System.out.println("Saturday");
        break;
      case 7:
        today = "Sunday";
        System.out.println("Sunday");
        break;
      default:
        today = "Invalid day";
        System.out.println("Invalid day");
    }
    return today;
  }
}


Step 4 : Create “TestMain” class, to run junit test cases.

package eclemma_tutorial;

import static org.junit.Assert.*;

import org.junit.Test;

public class TesMain {
  @Test
  public void testGetDay() {
    assertEquals("Monday", Main.getDay(1));
    assertEquals("Tuesday", Main.getDay(2));
    assertEquals("Wednesday", Main.getDay(3));
  }
}


Step 5: Now right click on TestMain.java file, click “Coverage As” -> “Junit Test”. It generates coverage report like below.




As you observe the report file, it is showing there is only 42.9% code coverage. It is because, we tested only case 1, 2 and 3. case 4, 5, 6 and default cases are not tested.
 Code in red colour represents uncovered code while testing.

To get more coverage, i just tested for all remaining(case 4, 5, 6 and default) cases. Result looks like below.

Using decorators
    1. Open the preferences dialog from Window → Preferences...
     2. Navigate to page General → Appearance → Label Decorators
     3. Select Java Code Coverage and press OK






No comments:

Post a Comment