Skip to main content

Jun 23, 2020 - 2 minute read - Java For Testers JUnit Java Maven JUnit Examples

JUnit HTML Reports

To generate JUnit HTML reports from maven builds we need to add the surefire plugin to generate the xml files and then the site and reporting plugins to create the html report.

Explanation Video

Watch on YouTube

Creating JUnit Surefire XML files

The JUnit HTML files are built from the .xml files created when running the test.

The surefire plugin will generate the .xml files in your Target folder when running mvn test.

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M4</version>
            </plugin>
        </plugins>
    </build>

Generating the HTML Report

To Generate the HTML report. We need to use mvn site

And in order for that to work we need to add a site build plugins and a reporting plugin.

   <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-site-plugin</artifactId>
                <version>3.7.1</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M4</version>
            </plugin>
        </plugins>
    </build>

    <!-- `mvn clean test site` to generate the junit html report-->
    <reporting>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-report-plugin</artifactId>
                <version>3.0.0-M4</version>
            </plugin>
        </plugins>
    </reporting>

This will work for both JUnit 4 and Junit 5.

mvn clean test site

I generally run mvn clean test site so that I don’t have any previous .xml files messing up the report.

Official Documentation

Source Example

You can find a project with a pom.xml that does this over here:

And the pom.xml

NOTES: I orginally thought I needed to add the Maven Project Info Reports Plugin in build or reporting but this seems to be added through a transitive dependency and is mentioned in the build any, so I haven’t listed it in the pom.xml. Leaving this as a note to self in case it turns out to be important. I wanted to try and create a minimal pom.xml but if you have issues feel free to try adding it.

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-project-info-reports-plugin</artifactId>
   <version>3.0.0</version>
</plugin>

You will need a Github account to comment. Or you can contact me with your comment.

I reserve the right to delete spam comments e.g. if your comment adds no value and its purpose is simply to create a backlink to another site offering training, or courses, etc.