Home » Core Java » junit » JUnit Code Coverage

About Alvin Reyes

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.

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.
 
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.

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.
 
pom.xml

<build>
		<plugins>
			<plugin>
				<groupId>org.jacoco</groupId>
				<artifactId>jacoco-maven-plugin</artifactId>
				<version>0.6.5.201403032054</version>
				<executions>
					<execution>
						<id>pre-unit-test</id>
						<goals>
							<goal>prepare-agent</goal>
						</goals>
						<configuration>
							<destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile>
							<propertyName>surefireArgLine</propertyName>
						</configuration>
					</execution>
					<execution>
						<id>post-unit-test</id>
						<phase>test</phase>
						<goals>
							<goal>report</goal>
						</goals>
						<configuration>
							<!-- Sets the path to the file which contains the execution data. -->
							<dataFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</dataFile>
							<!-- Sets the output directory for the code coverage report. -->
							<outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
						</configuration>
					</execution>
				</executions>
				<dependencies>
					<dependency>
						<groupId>commons-collections</groupId>
						<artifactId>commons-collections</artifactId>
						<version>3.2.1</version>
					</dependency>
				</dependencies>
			</plugin>
		</plugins>
	</build>

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

Download
You can download the full source code of this example here: junit-assert-coverage-example
(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of