Debian Codegraph @

src/test/java/com/ginsberg/junit/exit/assertions/SystemExitAssertionTest.java

raw
1 package com.ginsberg.junit.exit.assertions;
2
3 import org.junit.jupiter.api.Test;
4 import org.opentest4j.AssertionFailedError;
5
6 import static com.ginsberg.junit.exit.assertions.SystemExitAssertion.assertThatCallsSystemExit;
7 import static com.ginsberg.junit.exit.assertions.SystemExitAssertion.assertThatDoesNotCallSystemExit;
8 import static org.assertj.core.api.Assertions.assertThat;
9 import static org.junit.jupiter.api.Assertions.assertEquals;
10 import static org.junit.jupiter.api.Assertions.fail;
11
12 class SystemExitAssertionTest {
13
14 @Test
15 void catchesExit() {
16 assertThatCallsSystemExit(() -> System.exit(1));
17 }
18
19 @Test
20 void catchesExitWithCode() {
21 assertThatCallsSystemExit(() -> System.exit(2)).withExitCode(2);
22 }
23
24 @Test
25 void catchesExitWithCodeInRange() {
26 assertThatCallsSystemExit(() -> System.exit(3)).withExitCodeInRange(1, 3);
27 }
28
29 @Test
30 void catchesMultipleExits() {
31 assertThatCallsSystemExit(() -> {
32 System.exit(4);
33 System.exit(5);
34 System.exit(6);
35 }).withExitCode(4);
36 }
37
38 @Test
39 void exitCodeDoesNotMatch() {
40 try {
41 assertThatCallsSystemExit(() -> System.exit(5)).withExitCode(6);
42 fail("Should have failed test when System.exit was not called but expected");
43 } catch (AssertionFailedError e) {
44 assertThat(e.getMessage()).startsWith("Wrong exit code found");
45 }
46 }
47
48 @Test
49 void exitCodeNotInRangeHigh() {
50 try {
51 assertThatCallsSystemExit(() -> System.exit(7)).withExitCodeInRange(1, 6);
52 fail("Should have failed test when System.exit was not in range");
53 } catch (AssertionFailedError e) {
54 assertThat(e.getMessage()).startsWith("Exit code expected in range (1 .. 6) but was 7");
55 }
56 }
57
58 @Test
59 void exitCodeNotInRangeLow() {
60 try {
61 assertThatCallsSystemExit(() -> System.exit(8)).withExitCodeInRange(9, 11);
62 fail("Should have failed test when System.exit was not in range");
63 } catch (AssertionFailedError e) {
64 assertThat(e.getMessage()).startsWith("Exit code expected in range (9 .. 11) but was 8");
65 }
66 }
67
68 @Test
69 void expectingNoExit() {
70 assertThatDoesNotCallSystemExit(System::currentTimeMillis);
71 }
72
73 @Test
74 void expectingNoExitWhenExitHappens() {
75 try {
76 assertThatDoesNotCallSystemExit(() ->
77 System.exit(9)
78 );
79 fail("Should have failed test when System.exit was called but not expected");
80 } catch (AssertionFailedError e) {
81 assertThat(e.getMessage()).startsWith("Unexpected call to System.exit()");
82 }
83 }
84
85 @Test
86 void expectingSystemExitButSomethingElseThrown() {
87 try {
88 assertThatCallsSystemExit(() -> {
89 throw new IllegalStateException();
90 }).withExitCode(10);
91 } catch (final Exception e) {
92 assertEquals(IllegalStateException.class, e.getCause().getClass());
93 }
94 }
95
96 @Test
97 void failsWhenNoExit() {
98 try {
99 assertThatCallsSystemExit(System::currentTimeMillis).withExitCode(11);
100 fail("Should have failed test when System.exit was not called but expected");
101 } catch (AssertionFailedError e) {
102 assertThat(e.getMessage()).startsWith("Expected call to System.exit() did not happen");
103 }
104 }
105 }