diff --git a/CHANGELOG.md b/CHANGELOG.md index 818f33f..2d11c8e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/README.md b/README.md index 01a7511..fd4a74d 100644 --- a/README.md +++ b/README.md @@ -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 @@ -73,7 +73,7 @@ test { com.ginsberg junit5-system-exit - 2.0.1 + 2.0.2 test ``` diff --git a/VERSION.txt b/VERSION.txt index 10bf840..f93ea0c 100644 --- a/VERSION.txt +++ b/VERSION.txt @@ -1 +1 @@ -2.0.1 \ No newline at end of file +2.0.2 \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts index bc72d40..32b98ce 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -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") diff --git a/src/main/java/com/ginsberg/junit/exit/assertions/SystemExitAssertion.java b/src/main/java/com/ginsberg/junit/exit/assertions/SystemExitAssertion.java index 606b661..7aae686 100644 --- a/src/main/java/com/ginsberg/junit/exit/assertions/SystemExitAssertion.java +++ b/src/main/java/com/ginsberg/junit/exit/assertions/SystemExitAssertion.java @@ -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) { diff --git a/src/test/java/com/ginsberg/junit/exit/assertions/SystemExitAssertionTest.java b/src/test/java/com/ginsberg/junit/exit/assertions/SystemExitAssertionTest.java index a2cd698..9419d4f 100644 --- a/src/test/java/com/ginsberg/junit/exit/assertions/SystemExitAssertionTest.java +++ b/src/test/java/com/ginsberg/junit/exit/assertions/SystemExitAssertionTest.java @@ -5,6 +5,7 @@ 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; @@ -12,55 +13,55 @@ 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"); } } @@ -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()"); } } @@ -86,8 +87,8 @@ 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()); } } @@ -95,26 +96,10 @@ void expectingSystemExitButSomethingElseThrown() { @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); - } } \ No newline at end of file