Main Tutorials

JUnit 5 + Maven examples

junit 5 logo

This article shows you how to add JUnit 5 in a Maven project, nothing special, just add the JUnit 5 junit-jupiter-engine library and make sure the maven-surefire-plugin is at least version 2.22.0

Technologies used:

  • Maven 3.6
  • Java 8
  • JUnit 5.5.2

1. Maven + JUnit 5

1. Add the JUni 5 jupiter engine.

pom.xml

	<dependency>
		<groupId>org.junit.jupiter</groupId>
		<artifactId>junit-jupiter-engine</artifactId>
		<version>5.5.2</version>
		<scope>test</scope>
	</dependency>

2. To run tests in Maven, the maven-surefire-plugin must at least version 2.22.0

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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.mkyong.core</groupId>
    <artifactId>junit5-maven</artifactId>
    <version>1.0</version>

    <properties>
        <java.version>1.8</java.version>
        <junit-jupiter.version>5.5.2</junit-jupiter.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>

        <!-- junit 5 -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit-jupiter.version}</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>

            <!-- Need at least 2.22.0 to support JUnit 5 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M3</version>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

        </plugins>
    </build>

</project>

2. Maven Project

A simple Maven project structure.

project structure

3. JUnit 5

3.1 A simple unit test example.

MessageService.java

package com.mkyong.core;

public class MessageService {

    public static String get() {
        return "Hello JUnit 5";
    }

}

3.2 JUnit 5 simple Assertions test.

MessageServiceTest.java

package com.mkyong.core;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class MessageServiceTest {

    @DisplayName("Test MessageService.get()")
    @Test
    void testGet() {
        assertEquals("Hello JUnit 5", MessageService.get());
    }

}

3.3 Run in IntelliJ IDEA, output:

test in ide

4. mvn test

4.1 The Maven Surefire Plugin will scan and run the following test classes:


**/Test*.java
**/*Test.java
**/*Tests.java
**/*TestCase.java
Terminal

$ cd project 
$ mvn test 

[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.mkyong.core.MessageServiceTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, 
	Time elapsed: 0.02 s - in com.mkyong.core.MessageServiceTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  7.488 s
[INFO] Finished at: 2019-09-14T10:27:01+08:00
[INFO] ------------------------------------------------------------------------

The test result will be generated at project\target\surefire-reports, in both .txt and .xml format.

5. mvn site

5.1 It’s better to generate a project site to view the unit test result in HTML format. To add the surefire unit test report into the site, add the following reporting section.

pom.xml

	<build>
        <plugins>

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

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-site-plugin</artifactId>
                <version>3.8.2</version>
            </plugin>

        </plugins>
    </build>

    <reporting>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-report-plugin</artifactId>
            </plugin>
        </plugins>
    </reporting>
Terminal

$ cd project 
$ mvn site 

The project site will be generated at project\target\site, clicks index.html

mvn site

Clicks project reports -> surefire report

mvn site

Done.

Download Source Code

$ git clone https://github.com/mkyong/junit-examples
$ cd junit5-maven
$ mvn test

References

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
2 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Ann M.
2 years ago

Thank you for all your informative, easy to follow posts! This is another great post by you. I was hoping you would add how to add a custom listener to create screenshots on failure by overloading the RunListener with JUnit5 and Maven surefire reports.

Caleb
3 years ago

Thank you! This solved the issue I was having.