A logo showing the text blog.marcnuri.com
Español
Home»Java»JUnit 5 - How to run unit tests in Maven

Recent Posts

  • Fabric8 Kubernetes Client 7.2 is now available!
  • Connecting to an MCP Server from JavaScript using AI SDK
  • Connecting to an MCP Server from JavaScript using LangChain.js
  • The Future of Developer Tools: Adapting to Machine-Based Developers
  • Connecting to a Model Context Protocol (MCP) Server from Java using LangChain4j

Categories

  • Artificial Intelligence
  • Front-end
  • Go
  • Industry and business
  • Java
  • JavaScript
  • Legacy
  • Operations
  • Personal
  • Pet projects
  • Tools

Archives

  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • August 2024
  • June 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • February 2020
  • January 2020
  • December 2019
  • October 2019
  • September 2019
  • July 2019
  • March 2019
  • November 2018
  • July 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
  • February 2018
  • December 2017
  • July 2017
  • January 2017
  • December 2015
  • November 2015
  • December 2014
  • March 2014
  • February 2011
  • November 2008
  • June 2008
  • May 2008
  • April 2008
  • January 2008
  • November 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007

JUnit 5 - How to run unit tests in Maven

2023-08-29 in Java tagged Java / Testing / JUnit / JUnit 5 / Maven / Test-Driven Development (TDD) by Marc Nuri | Last updated: 2023-08-29
Versión en Español

Introduction

In this post, I'll show you how you can configure your Apache Maven project to execute JUnit 5 (or JUnit 4) tests using the Maven Surefire plugin.

Apache Maven is a build automation tool specially designed for Java projects (although you can use it for other languages too).

If we check the Maven homepage, we'll learn that Maven relies on plugins to perform most of its tasks. This is summarized by the following quote:

Maven is -at its heart- a plugin execution framework; all work is done by plugins.

The Maven plugin in charge of running unit tests is the Maven Surefire plugin. Let's learn how it works.

Configuring the Maven Surefire Plugin

Maven uses the Maven Surefire Plugin by default whenever we execute the mvn test goal. This means that we don't even need to configure the plugin for Maven to use it.

For example, consider a base project with the following pom.xml content:

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.marcnuri.demo</groupId>
  <artifactId>junit5-surefire</artifactId>
  <version>0.0-SNAPSHOT</version>
  <properties>
    <java.source.version>17</java.source.version>
    <java.target.version>17</java.target.version>
  </properties>
</project>

By executing the following command:

bash
mvn test

We should be able to see a message like:

[INFO] --- surefire:3.0.0:test (default-test) @ junit5-surefire ---
[INFO] No tests to run.

Since we don't have tests in the project, we get the No tests to run message.

You can notice in the logs that Maven is running version 3.0.0 of the Surefire plugin. This is because I'm running Maven 3.9.2 which uses this version of the Surefire plugin.

In the future, we might want to set an explicit version of the plugin or provide further configurations. For that purpose, we need to declare a plugin dependency in the build section of the pom.xml file:

pom.xml
<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>3.1.2</version>
    </plugin>
  </plugins>
</build>

If we run the mvn test command again, we'll notice that the surefire version has changed and it reports 3.1.2 instead:

[INFO] --- surefire:3.1.2:test (default-test) @ junit5-surefire ---
[INFO] No tests to run.

However, we still don't have any test to run. Let's now see how to add a JUnit 5 test to the project.

Creating JUnit 5 tests

Let's start by adding the JUnit 5 dependency to our project's pom.xml file:

pom.xml
<dependencies>
  <dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter</artifactId>
    <version>5.10.0</version>
    <scope>test</scope>
  </dependency>
</dependencies>

Next, let's create an example test class in the src/test/java directory with the name ExampleTest.java:

ExampleTest.java
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;

class ExampleTest {
  @Test
  void test() {
    assertTrue(true);
  }
}

This is a very simple test that will always pass. We're just asserting that true is true.

Note

The class name is important so that the Maven Surefire Plugin can include the test in the execution.

In this case, we've named the class ExampleTest with the Test suffix. You can further configure these suffixes in the Maven Plugin configuration.

Let's execute once more the mvn test command:

[INFO] --- surefire:3.1.2:test (default-test) @ junit5-surefire ---
[INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider
[INFO]
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running ExampleTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.032 s -- in ExampleTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

Maven Surefire plugin successfully detects the test we just implemented and executes it for us. The Surefire plugin also prints a very simple report with the name of the test class and the test results.

Conclusion

In this post, I've shown you how to prepare your Java Maven project to be able to run both JUnit 4 and JUnit 5 tests. This is the most basic structure that will enable you to add complexity in the future.

Twitter iconFacebook iconLinkedIn iconPinterest iconEmail icon

Post navigation
Eclipse JKube 1.14 is now available!JUnit 5 - How to disable or ignore tests
© 2007 - 2025 Marc Nuri