Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to use junit5-system-exit

I'm trying to write some unit tests for a command line application which makes use of System.exit(1) to signal a failed execution.

Since we are using Java 21, in order to avoid problems with the security manager as described here, we thought about using the approach proposed here, which uses the junit5-system-exit extension (site; GitHub).

Following the instructions from GitHub, my pom.xml looks like:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>21</maven.compiler.source>
    <maven.compiler.target>21</maven.compiler.target>
    <argLine/>
</properties>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.junit</groupId>
            <artifactId>junit-bom</artifactId>
            <version>5.11.3</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.ginsberg</groupId>
        <artifactId>junit5-system-exit</artifactId>
        <version>2.0.2</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>properties</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.5.2</version>
            <configuration>
                <argLine>@{argLine} -javaagent:${com.ginsberg:junit5-system-exit:jar}</argLine>
            </configuration>
        </plugin>
    </plugins>
</build>

However, when I execute the tests, it fails with the following message:

java.lang.IllegalStateException: SystemExitExtension Agent not loaded, please see documentation

So, it seems that I'm not able to configure Surefire properly. I have tried different approaches, but without luck. What am I doing wrong?

like image 452
Bob Rivers Avatar asked Dec 16 '25 21:12

Bob Rivers


1 Answers

I managed to make the extension work by changing the pom.xml.

Instead of using <dependencyManagement>, I imported the dependency directly to junit-jupiter-api. I also updated its version to 5.12.0.

I don't know if it was the pom.xml change, or if the previous version was corrupted in our local repository, but the fact is that now when I run maven, it works nicely and I'm able to test the system.exit calls.

like image 95
Bob Rivers Avatar answered Dec 19 '25 11:12

Bob Rivers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!