JUnit Code Coverage
1. Introduction
For all test cases, it is important that coverage always analyses the whole code. This is a definitive and statistical proof that all testable code is indeed tested. In this example, I’ll be showcasing how a developer can turn on and off their code coverage on their unit test cases.
2. Tools
For this example, we’ll be using the following:
3. Step by Step
3.1 Setup your Eclipse and install EclEmma
Install EclEmma on your Eclipse. You can download from the Eclipse Marketplace or go to here. As soon as you manage to install the plugin, an additional option on the project execution context menu will be available for code coverage.

3.2 Source code
The project that comes along with this example will have 2 sets of JUnit test cases. This is a Maven project and can be imported from an Eclipse work space with Maven plugin installed. Here is an example of the JUnit Test source code that we will use for this post.
JUnitTestAssertThatAssertions.java
package com.areyes1.junitassertthat.sample;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.BaseMatcher.*;
import java.util.ArrayList;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
public class JUnitTestAssertThatAssertions {
int totalNumberOfApplicants = 0;
int totalNumberOfAcceptableApplicants = 10;
ArrayList listOfValidStrings = new ArrayList();
@Before
public void setData(){
this.totalNumberOfApplicants = 9;
listOfValidStrings.add("object_1");
listOfValidStrings.add("object_2");
listOfValidStrings.add("object_3");
}
@Test
public void testAssertThatEqual() {
assertThat("123",is("123"));
}
@Test
public void testAssertThatNotEqual() {
assertThat(totalNumberOfApplicants,is(123));
}
@Test
public void testAssertThatObject() {
assertThat("123",isA(String.class));
}
@Test
public void testAssertThatWMessage(){
assertThat("They are not equal!","123",is("1234"));
}
}
3.3 Code Coverage
Basically, the tool runs the junit test and documents all source code (both junit and project source) and display the coverage level of each implementation method / class. This is extremely helpful in measuring the code quality and stability of your code. In this examples project, I ran the code coverage tool to see if the test cases I did, covered all the implementation methods of the project sources.

