junit5-system-exit
junit5-system-exit copied to clipboard
A JUnit5 Extension to help write tests that call System.exit()
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.1.2")
Maven
<dependency>
<groupId>com.ginsberg</groupId>
<artifactId>junit5-system-exit</artifactId>
<version>1.1.2</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);
}
}
A Test that should not expect System.exit(1)
to be called, and fails the test if it does:
public class MyTestCases {
@Test
@FailOnSystemExit
public void thisTestWillFail() {
System.exit(1);
}
}
The @ExpectSystemExit
, @ExpectSystemExitWithStatus
, and @FailOnSystemExit
annotations can be applied to methods, classes, 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 © 2021 by Todd Ginsberg