Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Change Log for `junit5-system-exit`

### 2.0.2
- Bugfix: [[#24]](https://github.com/tginsberg/junit5-system-exit/issues/24): Reset state between assertion calls.

### 2.0.1
- Bugfix: [[#20]](https://github.com/tginsberg/junit5-system-exit/issues/20): Multiple calls to `System.exit()` do not always report the first exit status code.

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ your test task. Please consult the [FAQ](#faq) below if you run into problems.
#### 1. Copy the following into your `build.gradle` or `build.gradle.kts`.

```groovy
testImplementation("com.ginsberg:junit5-system-exit:2.0.1")
testImplementation("com.ginsberg:junit5-system-exit:2.0.2")
```

#### 2. Enable the Java Agent
Expand Down Expand Up @@ -73,7 +73,7 @@ test {
<dependency>
<groupId>com.ginsberg</groupId>
<artifactId>junit5-system-exit</artifactId>
<version>2.0.1</version>
<version>2.0.2</version>
<scope>test</scope>
</dependency>
```
Expand Down
2 changes: 1 addition & 1 deletion VERSION.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.0.1
2.0.2
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ dependencies {
because("Starting in Gradle 9.0, this needs to be an explicitly declared dependency")
}

testImplementation("org.assertj:assertj-core:3.26.3")
testImplementation("org.junit.jupiter:junit-jupiter:$junitVersion")
testImplementation("org.junit.jupiter:junit-jupiter-params:$junitVersion")
testImplementation("org.junit.platform:junit-platform-launcher:$junitPlatformLauncherVersion")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ private SystemExitAssertion didNotCallSystemExit() {
private static SystemExitPreventedException catchSystemExitFrom(final Runnable function) {
final ExitPreventerStrategy exitPreventerStrategy = new AgentSystemExitHandlerStrategy();
try {
exitPreventerStrategy.resetBetweenTests();
exitPreventerStrategy.beforeTest();
function.run();
} catch (SystemExitPreventedException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,62 +5,63 @@

import static com.ginsberg.junit.exit.assertions.SystemExitAssertion.assertThatCallsSystemExit;
import static com.ginsberg.junit.exit.assertions.SystemExitAssertion.assertThatDoesNotCallSystemExit;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;

class SystemExitAssertionTest {

@Test
void catchesExit() {
assertThatCallsSystemExit(() -> System.exit(42));
assertThatCallsSystemExit(() -> System.exit(1));
}

@Test
void catchesExitWithCode() {
assertThatCallsSystemExit(() -> System.exit(42)).withExitCode(42);
assertThatCallsSystemExit(() -> System.exit(2)).withExitCode(2);
}

@Test
void catchesExitWithCodeInRange() {
assertThatCallsSystemExit(() -> System.exit(42)).withExitCodeInRange(41, 43);
assertThatCallsSystemExit(() -> System.exit(3)).withExitCodeInRange(1, 3);
}

@Test
void catchesMultipleExits() {
assertThatCallsSystemExit(() -> {
justExit();
justExit();
justExit();
}).withExitCode(42);
System.exit(4);
System.exit(5);
System.exit(6);
}).withExitCode(4);
}

@Test
void exitCodeDoesNotMatch() {
try {
assertThatCallsSystemExit(() -> System.exit(42)).withExitCode(43);
assertThatCallsSystemExit(() -> System.exit(5)).withExitCode(6);
fail("Should have failed test when System.exit was not called but expected");
} catch (AssertionFailedError e) {
// Expected
assertThat(e.getMessage()).startsWith("Wrong exit code found");
}
}

@Test
void exitCodeNotInRangeHigh() {
try {
assertThatCallsSystemExit(() -> System.exit(44)).withExitCodeInRange(41, 43);
assertThatCallsSystemExit(() -> System.exit(7)).withExitCodeInRange(1, 6);
fail("Should have failed test when System.exit was not in range");
} catch (AssertionFailedError e) {
// Expected
assertThat(e.getMessage()).startsWith("Exit code expected in range (1 .. 6) but was 7");
}
}

@Test
void exitCodeNotInRangeLow() {
try {
assertThatCallsSystemExit(() -> System.exit(40)).withExitCodeInRange(41,43);
assertThatCallsSystemExit(() -> System.exit(8)).withExitCodeInRange(9, 11);
fail("Should have failed test when System.exit was not in range");
} catch (AssertionFailedError e) {
// Expected
assertThat(e.getMessage()).startsWith("Exit code expected in range (9 .. 11) but was 8");
}
}

Expand All @@ -73,11 +74,11 @@ void expectingNoExit() {
void expectingNoExitWhenExitHappens() {
try {
assertThatDoesNotCallSystemExit(() ->
System.exit(42)
System.exit(9)
);
fail("Should have failed test when System.exit was called but not expected");
} catch (AssertionFailedError e) {
// Expected
assertThat(e.getMessage()).startsWith("Unexpected call to System.exit()");
}
}

Expand All @@ -86,35 +87,19 @@ void expectingSystemExitButSomethingElseThrown() {
try {
assertThatCallsSystemExit(() -> {
throw new IllegalStateException();
}).withExitCode(42);
} catch(final Exception e) {
}).withExitCode(10);
} catch (final Exception e) {
assertEquals(IllegalStateException.class, e.getCause().getClass());
}
}

@Test
void failsWhenNoExit() {
try {
assertThatCallsSystemExit(System::currentTimeMillis).withExitCode(42);
assertThatCallsSystemExit(System::currentTimeMillis).withExitCode(11);
fail("Should have failed test when System.exit was not called but expected");
} catch (AssertionFailedError e) {
// Expected
assertThat(e.getMessage()).startsWith("Expected call to System.exit() did not happen");
}
}

@Test
void multipleCallsToExit() {
assertThatCallsSystemExit(() -> {
try {
System.exit(42);
System.exit(1);
} catch (final Exception e) {
System.exit(2);
}
}).withExitCode(42);
}

private void justExit() {
System.exit(42);
}
}