Debian Codegraph @

src/test/java/com/ginsberg/junit/exit/ExpectSystemExitWithStatusTest.java

raw
1 /*
2 * MIT License
3 *
4 * Copyright (c) 2024 Todd Ginsberg
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25 package com.ginsberg.junit.exit;
26
27 import org.junit.jupiter.api.DisplayName;
28 import org.junit.jupiter.api.Nested;
29 import org.junit.jupiter.api.Test;
30 import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
31
32 import static com.ginsberg.junit.exit.TestUtils.assertTestFails;
33 import static com.ginsberg.junit.exit.TestUtils.assertTestFailsExceptionally;
34
35 class ExpectSystemExitWithStatusTest {
36
37 @Nested
38 @DisplayName("Success Cases")
39 class HappyPath {
40 @Test
41 @DisplayName("System.exit(1234) is caught and detected")
42 @ExpectSystemExitWithStatus(1234)
43 void detectSystemExit() {
44 System.exit(1234);
45 }
46
47 @Test
48 @DisplayName("System.exit(1234) is caught and detected within a thread")
49 @ExpectSystemExitWithStatus(1234)
50 void detectSystemExitInThread() throws InterruptedException {
51 final Thread t = new Thread(() -> System.exit(1234));
52 t.start();
53 t.join();
54 }
55
56 @Nested
57 @DisplayName("Class Success")
58 @ExpectSystemExitWithStatus(123456)
59 class ExpectedSuccessClassLevel {
60 @Test
61 @DisplayName("Method in class annotated with ExpectSystemExitWithStatus(123456) succeeds")
62 void classLevelExpect() {
63 System.exit(123456);
64 }
65 }
66 }
67
68 @Nested
69 @DisplayName("Failure Cases")
70 class FailurePath {
71 @Test
72 @DisplayName("System.exit(1234) is expected but not called at all, on method")
73 void expectSystemExitThatDoesNotHappenMethod() {
74 assertTestFails(ExpectedFailuresAtMethodLevelTest.class, "doNotCallSystemExit");
75 }
76
77 @Test
78 @DisplayName("System.exit(1234) is expected but another code was used, on method")
79 void expectSystemExitWithDifferentCodeMethod() {
80 assertTestFails(ExpectedFailuresAtMethodLevelTest.class, "exitWith4567");
81 }
82
83 @Test
84 @DisplayName("System.exit(1234) is expected but not called at all, on method")
85 void expectSystemExitThatDoesNotHappenClass() {
86 assertTestFails(ExpectedFailuresAtClassLevelNoExitTest.class);
87 }
88
89 @Test
90 @DisplayName("System.exit(1234) is expected but another code was used, on class")
91 void expectSystemExitWithDifferentCodeClass() {
92 assertTestFails(ExpectedFailuresAtClassLevelDifferentCodeTest.class);
93 }
94
95 @Test
96 @DisplayName("Exceptions other than SystemExitPreventedException bubble out")
97 void expectNonSystemExitExceptionsToBubbleOutAndFailTest() {
98 assertTestFailsExceptionally(
99 ExpectedExceptionalFailureTest.class,
100 "throwInsteadOfExit",
101 IllegalStateException.class
102 );
103 }
104 }
105
106 @SuppressWarnings("JUnitMalformedDeclaration")
107 @EnabledIfSystemProperty(named = "running_within_test", matches = "true")
108 static class ExpectedFailuresAtMethodLevelTest {
109
110 @Test
111 @ExpectSystemExitWithStatus(1234)
112 void doNotCallSystemExit() {
113 // Done! :)
114 }
115
116 @Test
117 @ExpectSystemExitWithStatus(1234)
118 void exitWith4567() {
119 System.exit(4567);
120 }
121 }
122
123 @SuppressWarnings("JUnitMalformedDeclaration")
124 @EnabledIfSystemProperty(named = "running_within_test", matches = "true")
125 @ExpectSystemExitWithStatus(1234)
126 static class ExpectedFailuresAtClassLevelNoExitTest {
127
128 @Test
129 void doNotCallSystemExit() {
130 // Done! :)
131 }
132
133 }
134
135 @SuppressWarnings("JUnitMalformedDeclaration")
136 @EnabledIfSystemProperty(named = "running_within_test", matches = "true")
137 @ExpectSystemExitWithStatus(1234)
138 static class ExpectedFailuresAtClassLevelDifferentCodeTest {
139 @Test
140 void exitWith4567() {
141 System.exit(4567);
142 }
143 }
144
145 @SuppressWarnings("JUnitMalformedDeclaration")
146 @EnabledIfSystemProperty(named = "running_within_test", matches = "true")
147 @ExpectSystemExit
148 static class ExpectedExceptionalFailureTest {
149 @Test
150 void throwInsteadOfExit() {
151 throw new IllegalStateException();
152 }
153 }
154
155 }