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

1
2
3
4
5
6
7
8
9
<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

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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.

Want to be a JUnit Master ?
Subscribe to our newsletter and download the JUnit Programming Cookbook right 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!

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 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 ....
I agree to the Terms and Privacy Policy

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
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button