0

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
    Show your current Gradle or Maven file. They determine how your directory structure must be set up. – johanneslink Jan 11 '22 at 19:20
  • I updated the question with pom.xml. its src/main/java/com/test/classes src/test/java/com/test/classes – gopiselvam RGS Jan 12 '22 at 11:00
  • The file is named `org.junit.jupiter.api.extension.Extension` as specified in the documentation you quote, so no `.txt`, `.properties`, or `.java` suffix. See also the documentation of [`java.util.ServiceLoader`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/ServiceLoader.html) – Mark Rotteveel Jan 12 '22 at 15:22

1 Answers1

2

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

johanneslink
  • 4,877
  • 1
  • 20
  • 37
  • I'm using Intellij. I have created META-INF/services folder inside src/main/resources. And inside that I just created a file **org.junit.jupiter.api.extension.Extension** as mentioned [here](https://stackoverflow.com/questions/17531625/how-to-include-a-config-file-in-the-meta-inf-services-folder-of-a-jar-using-ma). in file explorer It's showing type as EXTENSION. Now I'm unable to load the file using **ServiceLoader**. Could u please suggest where it went wrong? Is there any alternate approach to do that? – gopiselvam RGS Jan 13 '22 at 06:50
  • I am at a loss of what you want to achieve. You don’t have to load anything yourself through ServiceLoader. Jupiter is doing that for you. – johanneslink Jan 13 '22 at 13:00