JUNIT 5 Automatic Extension Registration | How to create Extensions file inside META-INF/services folder and what is the folder structure?

759 views Asked by At

I'm confused after reading JUnit 5 document

Junit Automatic Extension Registration

Specifically, a custom extension can be registered by supplying its fully qualified class name in a file named org.junit.jupiter.api.extension.Extension within the /META-INF/services folder in its enclosing JAR file.

Could anyone please explain to me what are folders need to be created in my project, where I need to keep those file and folders, and the extension of the Extension file (.txt, .properties, or .java)

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>org.example</groupId>
<artifactId>JUnitTempDemo</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
    <maven.compiler.source>8</maven.compiler.source>
    <maven.compiler.target>8</maven.compiler.target>
</properties>

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

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.6.2</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-runner</artifactId>
            <version>1.6.2</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-params</artifactId>
            <version>5.6.2</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

    </dependencies>
1

There are 1 answers

2
johanneslink On

In a standard Maven project service registrations are in folder

/src/main/resources/META-INF/services

So you'd have a file in this directory called org.junit.jupiter.api.extension.Extension with content:

your.project.junit5.extension.ExtensionImplementation

The class my.project.junit5.extension.ExtensionImplementation must be an implementation of one or more of JUnit-Jupiter's extension hooks.

But that's for global extensions only. The different kinds of Jupiter extensions are described here: https://junit.org/junit5/docs/snapshot/user-guide/#extensions