JUnit 5 – Test Reports in HTML
Last Updated :
25 Jul, 2024
In this article, we will discuss how to view Test reports of JUnit 5 in HTML formats. By default, JUnit 5 produces test reports in the form of XML files but understanding XML files directly is not possible and we will need some other third-party tool to parse the XML and give us the information about the test results from JUnit 5.
Generating and viewing Test reports in HTML format is very user-friendly to understand and use the test reports for understanding our test outputs. For creating JUnit 5 test reports in HTML format, we will use a maven plugin called "maven-surefire-report-plugin". This reporting plugin will parse the test output from JUnit 5 and create an HTML site for us in the "target/site" directory to view it in our browser without much hassle.
Prerequisite:
- Java
- JUnit 5
- Eclipse IDE and Maven
Project Structure:

Creating Test Reports in HTML
To create test reports in HTML format for JUnit 5 tests, add the below reporting plugin in your `pom.xml`. The maven-surefire-reporting-plugin will take the reports from "target/surefire-reports" generated by JUnit 5. By default, surefire reports will normally contain a text file and an XML file as our test report. But gathering data from XML and text files from surefire report is not viable, so we will use the reporting plugin to analyze the report and create a HTML site for us.
Step 1: Create a Maven Project
- Open the Eclipse IDE and create a basic Maven project.
- In the project pom.xml, add the below JUnit 5 dependencies and reporting plugin in your pom.xml file.
- This reporting plugin will generate the HTML reports of JUnit 5.
XML
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>junittestwithreport</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>junittestwithreport</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- maven compiler plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
</plugin>
<!-- plugin to create a maven site -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>4.0.0-M12</version>
</plugin>
</plugins>
</build>
<!-- Junit reporting plugin -->
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>3.2.2</version>
</plugin>
</plugins>
</reporting>
</project>
Now we have added the reporting plugin for the JUnit 5 testing project.
Step 2: Create a Utility class and a Test class
- In the src/main/java package, create a Java class that will provide a simple function so that it can be tested in JUnit 5.
- We will name it as MathUtil as it is going to provide a function to check a number is prime or not.
- Create the MathUtil class inside a package and add the below code.
Java
// MathUtil.java
package com.example.main;
public class MathUtil {
// Function to check a number is prime or not
public static boolean isPrime(int n)
{
// Loop through till sqrt of n
for (int val = 2; val * val <= n; val++) {
// check divisibility
if (n % val == 0)
return false;
}
// edge case condition for 1
return n > 1;
}
}
- Write a simple JUnit testcase to test the above utility class.
- Create a MathUtilTest class in the src/test/java/ directory.
- Write some JUnit test methods to test the isPrime function of MathUtil class.
Java
// MathUtilTest.java
package com.example.test;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import com.example.main.MathUtil;
import org.junit.jupiter.api.Test;
public class MathUtilTest {
// Test methods to test the isPrime method
@Test public void testAPrime()
{
// asserting the condition to be true
assertTrue(MathUtil.isPrime(11));
}
@Test public void testEvenNumberIsPrime()
{
assertFalse(MathUtil.isPrime(22));
}
@Test public void testNotPrime()
{
assertFalse(MathUtil.isPrime(80));
}
}
Step 3: Generating Test reports in HTML
Using Maven site Plugin:
mvn site
- The maven site plugin will generate more reports like dependency, plugins, summary along with Project test report.
- It also looks more modern than the HTML output of Maven Surefire Report Plugin.
Using Maven Surefire Report Plugin:
mvn surefire-report:report
- The surefire report will only generate the test reports in HTML no other information will be included like the Maven site.
- Both of the plugin will compile and test the project before generating the report, so you won't need to run mvn test and then proceed with the above commands.
Step 4: Viewing the HTML test reports.
- Once the above command run is done, the report will be saved under "target/site" directory of the project.
- The maven generated site for the project will be the index.html and the test reports will be found in surefire-report.html
Output:
The below output video shows the procedure of testing and viewing the JUnit 5 test reports in HTML using Maven site plugin.
The below image is a sample output of Maven surefire-report plugin, this looks slightly outdated but its an option too if you just need to view the Test reports of JUnit 5.

Conclusion
With that we have seen how we can create JUnit 5 Test Reports in HTML with maven-surefire-report-plugin. Add the plugin to your Java developer tool set to highly increase your productivity.
Similar Reads
JUnit 5 - Eclipse Test Templates
JUnit 5 simplifies the process of writing and executing test cases in Java applications. It offers enhanced support for modern Java features, increased extensibility, and a testing API that is more flexible and expressive. Test TemplatesA test template is a predefined format for writing test cases o
7 min read
JUnit 5 - XML Report Example
In Spring Boot, JUnit can be used for unit testing frameworks in Java, and Junit5 is the latest version. Junit provides a set of tools and APIs for developers to use to write and execute automated tests, and it is primarily used for unit testing Java applications. It is an open-source testing framew
6 min read
JUnit 5 â @RepeatedTest
JUnit 5 is a powerful and flexible testing framework in the Java ecosystem, widely used for unit testing Java applications. It introduces several modern features that simplify testing, making it a preferred choice for developers. One of its common features is the @RepeatedTest annotation, which is d
5 min read
JUnit 5 @Nested Test Classes
JUnit 5 introduced several powerful features to make testing in Java more flexible and expressive. One of these features is the @Nested test classes, which allows for better organization of test cases by logically grouping them inside an outer test class. This can be particularly useful when dealing
6 min read
JUnit 5 - Test Suites with Example
JUnit 5 encourages a modular approach to test creation with its test suites. These suites function as containers, bundling multiple test classes for easier management and collective execution within a single run. In this article, we will learn about JUnit 5 - Test Suites. Test SuiteIn JUnit 5, a tes
2 min read
JUnit â Ordered Tests
JUnit is a popular framework in the Java ecosystem designed for creating and executing automated tests. Although the framework encourages writing independent tests that can run in any order, there are scenarios where tests need to be executed in a specific order. This article explores how to order t
7 min read
JUnit 5 â Execute Test in Eclipse
JUnit is one of the widely used unit testing frameworks for Java-based Applications. We have different versions and upgrades published by JUnit 5, the latest version available in JUnit is JUnit 5. JUnit 5 is the upgraded version of JUnit 4, which is launched to support various latest features of Jav
10 min read
How to install JUnit 5 on Ubuntu?
JUnit 5 is a powerful and widely used testing framework for Java applications. Installing JUnit 5 on Ubuntu involves configuring a Java project with the necessary dependencies. Begin by setting up a Java project using a build tool such as Maven or Gradle, and then include JUnit 5 as a dependency. Th
4 min read
JUnit 5 - @ParameterizedTest
Parameterized tests in JUnit 5 provide the ability to run the same test multiple times with different inputs, helping to improve code coverage and catch edge cases that might otherwise go unnoticed By automating test execution with multiple sets of input data, parameterized tests simplify validation
4 min read
Tagging and Filtering JUnit Tests
JUnit is a widely used testing framework for Java applications that allows developers to write unit tests for their code. One of its powerful features is the ability to tag and filter tests. This feature is useful when running a subset of the tests under different conditions, such as during developm
9 min read