55
66import static com .ginsberg .junit .exit .assertions .SystemExitAssertion .assertThatCallsSystemExit ;
77import static com .ginsberg .junit .exit .assertions .SystemExitAssertion .assertThatDoesNotCallSystemExit ;
8+ import static org .assertj .core .api .Assertions .assertThat ;
89import static org .junit .jupiter .api .Assertions .assertEquals ;
910import static org .junit .jupiter .api .Assertions .fail ;
1011
1112class SystemExitAssertionTest {
1213
1314 @ Test
1415 void catchesExit () {
15- assertThatCallsSystemExit (() -> System .exit (42 ));
16+ assertThatCallsSystemExit (() -> System .exit (1 ));
1617 }
1718
1819 @ Test
1920 void catchesExitWithCode () {
20- assertThatCallsSystemExit (() -> System .exit (42 )).withExitCode (42 );
21+ assertThatCallsSystemExit (() -> System .exit (2 )).withExitCode (2 );
2122 }
2223
2324 @ Test
2425 void catchesExitWithCodeInRange () {
25- assertThatCallsSystemExit (() -> System .exit (42 )).withExitCodeInRange (41 , 43 );
26+ assertThatCallsSystemExit (() -> System .exit (3 )).withExitCodeInRange (1 , 3 );
2627 }
2728
2829 @ Test
2930 void catchesMultipleExits () {
3031 assertThatCallsSystemExit (() -> {
31- justExit ( );
32- justExit ( );
33- justExit ( );
34- }).withExitCode (42 );
32+ System . exit ( 4 );
33+ System . exit ( 5 );
34+ System . exit ( 6 );
35+ }).withExitCode (4 );
3536 }
3637
3738 @ Test
3839 void exitCodeDoesNotMatch () {
3940 try {
40- assertThatCallsSystemExit (() -> System .exit (42 )).withExitCode (43 );
41+ assertThatCallsSystemExit (() -> System .exit (5 )).withExitCode (6 );
4142 fail ("Should have failed test when System.exit was not called but expected" );
4243 } catch (AssertionFailedError e ) {
43- // Expected
44+ assertThat ( e . getMessage ()). startsWith ( "Wrong exit code found" );
4445 }
4546 }
4647
4748 @ Test
4849 void exitCodeNotInRangeHigh () {
4950 try {
50- assertThatCallsSystemExit (() -> System .exit (44 )).withExitCodeInRange (41 , 43 );
51+ assertThatCallsSystemExit (() -> System .exit (7 )).withExitCodeInRange (1 , 6 );
5152 fail ("Should have failed test when System.exit was not in range" );
5253 } catch (AssertionFailedError e ) {
53- // Expected
54+ assertThat ( e . getMessage ()). startsWith ( "Exit code expected in range (1 .. 6) but was 7" );
5455 }
5556 }
5657
5758 @ Test
5859 void exitCodeNotInRangeLow () {
5960 try {
60- assertThatCallsSystemExit (() -> System .exit (40 )).withExitCodeInRange (41 , 43 );
61+ assertThatCallsSystemExit (() -> System .exit (8 )).withExitCodeInRange (9 , 11 );
6162 fail ("Should have failed test when System.exit was not in range" );
6263 } catch (AssertionFailedError e ) {
63- // Expected
64+ assertThat ( e . getMessage ()). startsWith ( "Exit code expected in range (9 .. 11) but was 8" );
6465 }
6566 }
6667
@@ -73,11 +74,11 @@ void expectingNoExit() {
7374 void expectingNoExitWhenExitHappens () {
7475 try {
7576 assertThatDoesNotCallSystemExit (() ->
76- System .exit (42 )
77+ System .exit (9 )
7778 );
7879 fail ("Should have failed test when System.exit was called but not expected" );
7980 } catch (AssertionFailedError e ) {
80- // Expected
81+ assertThat ( e . getMessage ()). startsWith ( "Unexpected call to System.exit()" );
8182 }
8283 }
8384
@@ -86,35 +87,19 @@ void expectingSystemExitButSomethingElseThrown() {
8687 try {
8788 assertThatCallsSystemExit (() -> {
8889 throw new IllegalStateException ();
89- }).withExitCode (42 );
90- } catch (final Exception e ) {
90+ }).withExitCode (10 );
91+ } catch (final Exception e ) {
9192 assertEquals (IllegalStateException .class , e .getCause ().getClass ());
9293 }
9394 }
9495
9596 @ Test
9697 void failsWhenNoExit () {
9798 try {
98- assertThatCallsSystemExit (System ::currentTimeMillis ).withExitCode (42 );
99+ assertThatCallsSystemExit (System ::currentTimeMillis ).withExitCode (11 );
99100 fail ("Should have failed test when System.exit was not called but expected" );
100101 } catch (AssertionFailedError e ) {
101- // Expected
102+ assertThat ( e . getMessage ()). startsWith ( " Expected call to System.exit() did not happen" );
102103 }
103104 }
104-
105- @ Test
106- void multipleCallsToExit () {
107- assertThatCallsSystemExit (() -> {
108- try {
109- System .exit (42 );
110- System .exit (1 );
111- } catch (final Exception e ) {
112- System .exit (2 );
113- }
114- }).withExitCode (42 );
115- }
116-
117- private void justExit () {
118- System .exit (42 );
119- }
120105}
0 commit comments