Open In App

JUnit 5 – Test Reports in HTML

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

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




    <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




// 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;
    }
}




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

Using Maven Surefire Report Plugin:

mvn surefire-report:report

Step 4: Viewing the HTML test reports.

Output:

The below output 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.


Article Tags :