24
24
import java .util .Set ;
25
25
import java .util .concurrent .atomic .AtomicReference ;
26
26
27
+ import javax .annotation .PostConstruct ;
28
+
27
29
import org .junit .After ;
28
30
import org .junit .Before ;
29
31
import org .junit .Rule ;
74
76
import static org .hamcrest .Matchers .instanceOf ;
75
77
import static org .hamcrest .Matchers .is ;
76
78
import static org .hamcrest .Matchers .isA ;
79
+ import static org .hamcrest .Matchers .not ;
77
80
import static org .hamcrest .Matchers .sameInstance ;
78
81
import static org .hamcrest .Matchers .startsWith ;
79
82
import static org .junit .Assert .assertArrayEquals ;
83
86
import static org .junit .Assert .assertThat ;
84
87
import static org .junit .Assert .assertTrue ;
85
88
import static org .junit .Assert .fail ;
89
+ import static org .mockito .Matchers .any ;
86
90
import static org .mockito .Matchers .anyObject ;
87
91
import static org .mockito .Mockito .atLeastOnce ;
88
92
import static org .mockito .Mockito .mock ;
@@ -615,6 +619,31 @@ SpringBootExceptionHandler getSpringBootExceptionHandler() {
615
619
assertThat (listener .getExitCode (), equalTo (11 ));
616
620
}
617
621
622
+ @ Test
623
+ public void exceptionFromRefreshIsHandledGracefully () throws Exception {
624
+ final SpringBootExceptionHandler handler = mock (SpringBootExceptionHandler .class );
625
+ SpringApplication application = new SpringApplication (
626
+ RefreshFailureConfig .class ) {
627
+
628
+ @ Override
629
+ SpringBootExceptionHandler getSpringBootExceptionHandler () {
630
+ return handler ;
631
+ }
632
+
633
+ };
634
+ ExitCodeListener listener = new ExitCodeListener ();
635
+ application .addListeners (listener );
636
+ application .setWebEnvironment (false );
637
+ try {
638
+ application .run ();
639
+ fail ("Did not throw" );
640
+ }
641
+ catch (RuntimeException ex ) {
642
+ }
643
+ verify (handler ).registerLoggedException (any (RefreshFailureException .class ));
644
+ assertThat (this .output .toString (), not (containsString ("NullPointerException" )));
645
+ }
646
+
618
647
@ Test
619
648
public void defaultCommandLineArgs () throws Exception {
620
649
SpringApplication application = new SpringApplication (ExampleConfig .class );
@@ -966,6 +995,15 @@ public int getExitCode(Throwable exception) {
966
995
967
996
}
968
997
998
+ @ Configuration
999
+ static class RefreshFailureConfig {
1000
+
1001
+ @ PostConstruct
1002
+ public void fail () {
1003
+ throw new RefreshFailureException ();
1004
+ }
1005
+ }
1006
+
969
1007
static class ExitStatusException extends RuntimeException
970
1008
implements ExitCodeGenerator {
971
1009
@@ -976,6 +1014,10 @@ public int getExitCode() {
976
1014
977
1015
}
978
1016
1017
+ static class RefreshFailureException extends RuntimeException {
1018
+
1019
+ }
1020
+
979
1021
static class AbstractTestRunner implements ApplicationContextAware , Ordered {
980
1022
981
1023
private final String [] expectedBefore ;
0 commit comments