Debian Codegraph @

src/test/java/com/ginsberg/junit/exit/FailOnSystemExitTest.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.Test;
29 import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
30
31 import static com.ginsberg.junit.exit.TestUtils.assertTestFails;
32 import static com.ginsberg.junit.exit.TestUtils.assertTestSucceeds;
33
34 class FailOnSystemExitTest {
35
36 @Test
37 @DisplayName("@FailOnSystemExit on method - exception caught and fails test")
38 void failOnSystemExitOnMethod() {
39 assertTestFails(FailOnSystemExitAtTestLevelTest.class, "callsSystemExit");
40 }
41
42 @Test
43 @DisplayName("@FailOnSystemExit on method - System.exit not called")
44 void succeedWhenNotCallingSystemExitInMethod() {
45 assertTestSucceeds(FailOnSystemExitAtTestLevelTest.class, "doesNotCallSystemExit");
46 }
47
48 @Test
49 @DisplayName("@FailOnSystemExit on class - exception caught and fails test")
50 void failOnSystemExitOnClass() {
51 assertTestFails(FailOnSystemExitAtClassLevelTest.class);
52 }
53
54 @Test
55 @DisplayName("@FailOnSystemExit on class - System.exit not called")
56 void succeedWhenNotCallingSystemExitOnClass() {
57 assertTestSucceeds(FailOnSystemExitAtClassLevelWithoutSystemExitTest.class);
58 }
59
60 @SuppressWarnings("JUnitMalformedDeclaration")
61 @EnabledIfSystemProperty(named = "running_within_test", matches = "true")
62 static class FailOnSystemExitAtTestLevelTest {
63 @Test
64 @FailOnSystemExit
65 void callsSystemExit() {
66 System.exit(42);
67 }
68
69 @Test
70 @FailOnSystemExit
71 void doesNotCallSystemExit() {
72 // Nothing to do
73 }
74 }
75
76 @SuppressWarnings("JUnitMalformedDeclaration")
77 @EnabledIfSystemProperty(named = "running_within_test", matches = "true")
78 @FailOnSystemExit
79 static class FailOnSystemExitAtClassLevelTest {
80 @Test
81 void callsSystemExit() {
82 System.exit(42);
83 }
84 }
85
86 @SuppressWarnings("JUnitMalformedDeclaration")
87 @EnabledIfSystemProperty(named = "running_within_test", matches = "true")
88 @FailOnSystemExit
89 static class FailOnSystemExitAtClassLevelWithoutSystemExitTest {
90 @Test
91 void doesNotCallSystemExit() {
92 // Nothing to do
93 }
94 }
95
96
97 }