/junit5-system-exit

A JUnit5 Extension to help write tests that call System.exit()

Primary LanguageJavaMIT LicenseMIT

JUnit5 System.exit() Extension

This JUnit 5 Extension helps you write tests for code that calls System.exit(). Starting with JUnit 5, @Rules, @ClassRules, and Runners were replaced by the Extension concept.

Installing

Copy the following into your build.gradle or build.xml.

Gradle

testImplementation("com.ginsberg:junit5-system-exit:1.0.0")

Maven

<dependency>
    <groupId>com.ginsberg</groupId>
    <artifactId>junit5-system-exit</artifactId>
    <version>1.0.0</version>
    <scope>test</scope>
</dependency>

Use cases

A Test that expects System.exit() to be called, with any status code:

public class MyTestCases { 
    
    @Test
    @ExpectSystemExit
    public void thatSystemExitIsCalled() {
        System.exit(1);
    }
}

A Test that expects System.exit(1) to be called, with a specific status code:

public class MyTestCases {
    
    @Test
    @ExpectSystemExitWithStatus(1)
    public void thatSystemExitIsCalled() {
        System.exit(1);
    }
}

The @ExpectSystemExit and @ExpectSystemExitWithStatus annotations can be applied to methods, classs, or annotations (to act as meta-annotations).

Contributing and Issues

Please feel free to file issues for change requests or bugs. If you would like to contribute new functionality, please contact me first!

Copyright © 2018 by Todd Ginsberg