|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2024 the original author or authors. |
| 2 | + * Copyright 2012-2025 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
51 | 51 | import org.springframework.boot.testsupport.system.CapturedOutput;
|
52 | 52 | import org.springframework.boot.testsupport.system.OutputCaptureExtension;
|
53 | 53 | import org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer;
|
| 54 | +import org.springframework.boot.web.server.WebServer; |
54 | 55 | import org.springframework.boot.web.servlet.DelegatingFilterProxyRegistrationBean;
|
55 | 56 | import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
56 | 57 | import org.springframework.boot.web.servlet.ServletContextInitializer;
|
|
81 | 82 | import static org.mockito.ArgumentMatchers.anyString;
|
82 | 83 | import static org.mockito.BDDMockito.given;
|
83 | 84 | import static org.mockito.BDDMockito.then;
|
| 85 | +import static org.mockito.BDDMockito.willThrow; |
84 | 86 | import static org.mockito.Mockito.atMost;
|
85 | 87 | import static org.mockito.Mockito.inOrder;
|
86 | 88 | import static org.mockito.Mockito.mock;
|
@@ -211,6 +213,50 @@ void whenContextIsNotActiveThenCloseDoesNotChangeTheApplicationAvailability() {
|
211 | 213 | assertThat(listener.receivedEvents()).isEmpty();
|
212 | 214 | }
|
213 | 215 |
|
| 216 | + @Test |
| 217 | + void whenContextRefreshFailedThenWebServerIsStoppedAndDestroyed() { |
| 218 | + addWebServerFactoryBean(); |
| 219 | + TestApplicationListener listener = new TestApplicationListener(); |
| 220 | + this.context.addApplicationListener(listener); |
| 221 | + this.context.registerBeanDefinition("refreshFailure", new RootBeanDefinition(RefreshFailure.class)); |
| 222 | + assertThatExceptionOfType(BeanCreationException.class).isThrownBy(this.context::refresh); |
| 223 | + WebServer webServer = this.context.getWebServer(); |
| 224 | + then(webServer).should(times(2)).stop(); |
| 225 | + then(webServer).should().destroy(); |
| 226 | + } |
| 227 | + |
| 228 | + @Test |
| 229 | + void whenContextRefreshFailedThenWebServerStopFailedCatchStopException() { |
| 230 | + addWebServerFactoryBean(); |
| 231 | + this.context.registerBeanDefinition("refreshFailure", new RootBeanDefinition(RefreshFailure.class, () -> { |
| 232 | + willThrow(new RuntimeException("WebServer has failed to stop")).willCallRealMethod() |
| 233 | + .given(this.context.getWebServer()) |
| 234 | + .stop(); |
| 235 | + return new RefreshFailure(); |
| 236 | + })); |
| 237 | + assertThatExceptionOfType(BeanCreationException.class).isThrownBy(this.context::refresh) |
| 238 | + .withStackTraceContaining("WebServer has failed to stop"); |
| 239 | + WebServer webServer = this.context.getWebServer(); |
| 240 | + then(webServer).should().stop(); |
| 241 | + then(webServer).should(times(0)).destroy(); |
| 242 | + } |
| 243 | + |
| 244 | + @Test |
| 245 | + void whenContextRefreshFailedThenWebServerIsStoppedAndDestroyFailedCatchDestroyException() { |
| 246 | + addWebServerFactoryBean(); |
| 247 | + this.context.registerBeanDefinition("refreshFailure", new RootBeanDefinition(RefreshFailure.class, () -> { |
| 248 | + willThrow(new RuntimeException("WebServer has failed to destroy")).willCallRealMethod() |
| 249 | + .given(this.context.getWebServer()) |
| 250 | + .destroy(); |
| 251 | + return new RefreshFailure(); |
| 252 | + })); |
| 253 | + assertThatExceptionOfType(BeanCreationException.class).isThrownBy(this.context::refresh) |
| 254 | + .withStackTraceContaining("WebServer has failed to destroy"); |
| 255 | + WebServer webServer = this.context.getWebServer(); |
| 256 | + then(webServer).should().stop(); |
| 257 | + then(webServer).should().destroy(); |
| 258 | + } |
| 259 | + |
214 | 260 | @Test
|
215 | 261 | void cannotSecondRefresh() {
|
216 | 262 | addWebServerFactoryBean();
|
|
0 commit comments