junit

JUnit Report Example

Photo of Alvin ReyesAlvin ReyesJuly 20th, 2015Last Updated: March 21st, 2019
0 598 2 minutes read

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

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;@Beforepublic void setData() {serviceObject = new ServiceObject();junitAssertEqualsServiceSample = new JUnitReportServiceExample();junitAssertEqualsServiceSample.initiateMetaData(serviceObject);}@Testpublic void testAssertEqualsFalse() {//processed the itemServiceObject newServiceObject = new ServiceObject();junitAssertEqualsServiceSample.initiateMetaData(newServiceObject);junitAssertEqualsServiceSample.processObject(serviceObject);assertEquals(serviceObject,newServiceObject);}@Testpublic void testAssertEquals() {junitAssertEqualsServiceSample.processObject(serviceObject);assertEquals(serviceObject,this.serviceObject);}@Testpublic void testAssertEqualsWithMessage() {junitAssertEqualsServiceSample.processObject(serviceObject);assertEquals("Same Object",serviceObject,serviceObject);}@Testpublic 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.

Want to be a JUnit Master ?
Subscribe to our newsletter and download theJUnitProgramming Cookbookright now!
In order to help you master unit testing with JUnit, we have compiled a kick-ass guide with all the major JUnit features and use cases! Besides studying them online you may download the eBook in PDF format!

Thank you!

We will contact you soon.

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
Do you want to know how to develop your skillset to become aJava Rockstar?
Subscribe to our newsletter to start Rockingright now!
To get you started we give you our best selling eBooks forFREE!
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 ....
I agree to theTerms andPrivacy Policy

Thank you!

We will contact you soon.

Photo of Alvin ReyesAlvin ReyesJuly 20th, 2015Last Updated: March 21st, 2019
0 598 2 minutes read
Photo of 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.
Subscribe
Notify of
guest
I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.