How to Declare the JUnit 5 Maven Dependency in Your pom.xml File

You will not find JUnit 5 Maven dependencies in the pytest repository because pytest is a pure Python testing framework; instead, declare the org.junit.jupiter artifacts in your Java project's pom.xml file with test scope to enable JUnit 5 testing.

The pytest-dev/pytest repository is a Python-based testing framework that contains no Maven pom.xml files or Java artifacts. If you are building a Java project and need to add the JUnit 5 Maven dependency declarations to your pom.xml, you must reference the official org.junit.jupiter group ID. This guide provides the exact XML configuration required to integrate JUnit 5 into your Maven build.

Understanding the pytest Repository Context

The pytest codebase contains no Java source files or Maven configuration. Specifically, the repository lacks any pom.xml files because it is a pure Python project. However, pytest does provide JUnit-compatible XML output functionality through src/_pytest/junitxml.py, which generates test reports in a format that CI servers can consume. This module is unrelated to Java dependency management, confirming that you must source JUnit 5 artifacts externally from Maven Central.

Declaring JUnit 5 Maven Dependencies in pom.xml

To add JUnit 5 Maven dependency support, include the Jupiter API and Engine artifacts in your pom.xml dependencies section.

Core JUnit Jupiter API and Engine

The JUnit Jupiter API provides the annotations and assertions needed to write tests, while the JUnit Jupiter Engine discovers and executes them at runtime.

<dependencies>
    <!-- JUnit Jupiter API for writing tests -->
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.10.2</version>
        <scope>test</scope>
    </dependency>

    <!-- JUnit Jupiter Engine for running tests -->
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.10.2</version>
        <scope>test</scope>
    </dependency>
</dependencies>

Optional JUnit Platform Suite Engine

If you use the @Suite annotation to aggregate multiple test classes, add the JUnit Platform Suite Engine from the org.junit.platform group.

<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-suite-engine</artifactId>
    <version>1.10.2</version>
    <scope>test</scope>
</dependency>

Configuring the Maven Surefire Plugin

The Maven Surefire Plugin requires explicit configuration to invoke the JUnit Platform. Without this, Maven will not execute JUnit 5 tests during the test phase.

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.1.2</version>
            <configuration>
                <includes>
                    <include>**/*Test.java</include>
                    <include>**/*Tests.java</include>
                    <include>**/*TestCase.java</include>
                </includes>
            </configuration>
        </plugin>
    </plugins>
</build>

Complete pom.xml Example for JUnit 5

Below is a complete, runnable pom.xml skeleton that incorporates all JUnit 5 Maven dependency declarations and the required Surefire configuration.

<?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.example</groupId>
    <artifactId>junit5-demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <junit.jupiter.version>5.10.2</junit.jupiter.version>
    </properties>

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

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

Key Configuration Details for JUnit 5 Maven Dependencies

When declaring JUnit 5 Maven dependency entries, adhere to these specific conventions to ensure correct test execution.

  • groupId: Must be org.junit.jupiter for the Jupiter API and Engine, or org.junit.platform for platform-specific engines like the Suite Engine.
  • artifactId: Use junit-jupiter-api for writing tests and junit-jupiter-engine for running them. Do not confuse these with the legacy junit:junit artifact.
  • scope: Always set to test so that JUnit 5 remains a test-only dependency and is not bundled into production artifacts.
  • version alignment: Keep the API and Engine versions identical to prevent runtime linkage errors. Use a Maven property like ${junit.jupiter.version} to enforce this.
  • Surefire plugin: Version 2.22.0 or higher is required; version 3.x is recommended for full JUnit 5 support.

Summary

  • The pytest-dev/pytest repository is a Python testing framework and contains no JUnit 5 Maven dependency declarations or pom.xml files.
  • To use JUnit 5 in a Java project, declare dependencies with groupId org.junit.jupiter, artifactId junit-jupiter-api and junit-jupiter-engine, and scope test.
  • Align the versions of the API and Engine artifacts to prevent runtime conflicts.
  • Configure the Maven Surefire Plugin (version 3.x recommended) to enable JUnit Platform support during the mvn test phase.

Frequently Asked Questions

Why can't I find JUnit 5 dependencies in the pytest repository?

The pytest-dev/pytest repository implements a testing framework for Python code. It does not ship Java artifacts or Maven pom.xml files. While the repository contains src/_pytest/junitxml.py to generate JUnit-compatible XML reports, this module is purely for output formatting and unrelated to Java dependency management.

What is the difference between junit-jupiter-api and junit-jupiter-engine?

The junit-jupiter-api artifact provides the programming model and annotations—such as @Test, @BeforeEach, and Assertions—that you use to write test classes. The junit-jupiter-engine artifact implements the TestEngine interface that the JUnit Platform uses to discover and execute those tests at runtime. You need both dependencies to compile and run JUnit 5 tests.

Do I need the Maven Surefire plugin to run JUnit 5 tests?

Yes. The Maven Surefire Plugin is required to invoke the JUnit Platform during the test phase. Versions prior to 2.22.0 do not support JUnit 5 natively. It is recommended to use version 3.1.2 or newer to ensure full compatibility with the JUnit Jupiter engine and modern Java versions.

Can I use JUnit 5 with older versions of Maven?

You can use JUnit 5 with Maven 3.6.0 or higher, provided you configure the Maven Surefire Plugin to at least version 2.22.0. However, for optimal support and access to the latest JUnit 5 features, upgrade to Maven 3.8.x or newer and use Surefire 3.x. This ensures the JUnit Platform launcher is correctly integrated into the build lifecycle.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →