junit

JUnit Report Example

1. Introduction

In all the test cases of an application, there is always a way to create a report of it one way or the other. JUnit is not an exception to this and there’s a ton of ways to create reports from it. It can even be incorporated to the maven site and have it part of the overall java tech documentations of a Java Application.

One of the most widely used JUnit reporting plugin is the surefire and in this post, I’ll be showcasing how it can be used on your JUnit test cases.
 
 
 

2. The Surefire reporting  plugin

The Surefire Report Plugin parses the generated TEST-*.xml files under ${basedir}/target/surefire-reports and renders them using DOXIA, which creates the web interface version of the test results. It also allows generating reports in HTML format which will be the focus of our samples in this post. You can check the surefire plugin from here.

To configure your project to have surefire plugin, just include the following reporting plugin on the pom.xml of your maven project.

pom.xml

  <reporting>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-report-plugin</artifactId>
        <version>2.18.1</version>
      </plugin>
    </plugins>
  </reporting>

This will allow your project to call the surefire plugin goals to generate the HTML report.

3. Source

Let’s try creating a JUnit Test case and run the surefire report generation.

JUnitReportServiceExampleTest.java

package com.areyes1.junitreport.service;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;
public class JUnitReportServiceExampleTest {


	private JUnitReportServiceExample junitAssertEqualsServiceSample;
	private ServiceObject serviceObject;
	@Before
	public void setData() {
		serviceObject = new ServiceObject();
		junitAssertEqualsServiceSample = new JUnitReportServiceExample();
		junitAssertEqualsServiceSample.initiateMetaData(serviceObject);
	}

	@Test
	public void testAssertEqualsFalse() {
		//	processed the item
		ServiceObject newServiceObject = new ServiceObject();
		junitAssertEqualsServiceSample.initiateMetaData(newServiceObject);
		junitAssertEqualsServiceSample.processObject(serviceObject);
		assertEquals(serviceObject,newServiceObject);
	}
	
	@Test
	public void testAssertEquals() {
		junitAssertEqualsServiceSample.processObject(serviceObject);
		assertEquals(serviceObject,this.serviceObject);
	}

	@Test
	public void testAssertEqualsWithMessage() {
		junitAssertEqualsServiceSample.processObject(serviceObject);
		assertEquals(
				"Same Object",
				serviceObject,serviceObject);
	}
	@Test
	public void testAssertEqualsFalseWithMessage() {
		ServiceObject newServiceObject = new ServiceObject();
		junitAssertEqualsServiceSample.postProcessing(serviceObject);
		assertEquals(
				"Not the Same Object",
				newServiceObject,serviceObject);
	}


}

We create several test cases here that will succeed and fail. We then run the following command to generate the report.

mvn clean install test surefire-report:report

The report will be generated under the target folder of your maven project.

Figure 1.0 Surefire HTML and XML under the target folder
Figure 1.0 Surefire HTML and XML under the target folder

4. Reports

Here is an example of a report generated by the plugin.

Figure 2.0 JUnit HTML report
Figure 2.0 JUnit HTML report

It gives you the number of succeeded and failed test cases as well as the percentage covered (coverage) of your test cases. It’s a pretty neat reporting for projects that are keen to their quality of testing per build cycles. I see this as an extremely great feature for any Java application as the success of the test cases is directly proportional to the quality of the functionality of the application.

5. Download the Eclipse project

This was an example about JUnit Report.

Download
You can download the full source code of this example here : junit-report-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.
Subscribe
Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments
Back to top button