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:

  1. Eclipse
  2. EclEmma plugin for eclipse – code coverage tool
  3. Maven
  4. JaCoCo – Maven test suite plugin

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.

Figure 1.0 Code coverage plugin tool
Figure 1.0 Code coverage plugin tool

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.

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();
	public void setData(){
		this.totalNumberOfApplicants = 9;
	public void testAssertThatEqual() {
	public void testAssertThatNotEqual() {
	public void testAssertThatObject() {
	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.

Figure 2.0 Code Coverage report
Figure 2.0 Code Coverage report

Not only there is a tabular representation, it also highlights the lines of codes that are covered (green) and not as shown below.

Figore 3.0 Code coverage highlight
Figore 3.0 Code coverage highlight

3.4 Running JaCoCo to generate code coverage results via maven

We can actually create a code coverage report via maven by using JaCoCo plugin. Specify the following plugins in your pom.xml below and run the test cases.

							<!-- Sets the path to the file which contains the execution data. -->
							<!-- Sets the output directory for the code coverage report. -->

Run you test in maven using the following command:

mvn clean test

4. Download the Eclipse project

This was an example of JUnit Code Coverage

You can download the full source code of this example here: junit-assert-coverage-example

Alvin Reyes

Alvin has an Information Technology Degree from Mapua Institute of Technology. During his studies, he was already heavily involved in a number of small to large projects where he primarily contributes by doing programming, analysis design. After graduating, he continued to do side projects on Mobile, Desktop and Web Applications.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button