|
24 | 24 | import org.springframework.beans.factory.FactoryBean;
|
25 | 25 | import org.springframework.beans.factory.config.BeanDefinition;
|
26 | 26 | import org.springframework.beans.factory.support.RootBeanDefinition;
|
| 27 | +import org.springframework.context.ApplicationContextException; |
27 | 28 | import org.springframework.context.Lifecycle;
|
28 | 29 | import org.springframework.context.LifecycleProcessor;
|
29 | 30 | import org.springframework.context.SmartLifecycle;
|
30 | 31 | import org.springframework.core.testfixture.EnabledForTestGroups;
|
31 | 32 |
|
32 | 33 | import static org.assertj.core.api.Assertions.assertThat;
|
| 34 | +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
33 | 35 | import static org.springframework.core.testfixture.TestGroup.LONG_RUNNING;
|
34 | 36 |
|
35 | 37 | /**
|
@@ -107,6 +109,21 @@ void singleSmartLifecycleAutoStartupWithLazyInitFactoryBean() {
|
107 | 109 | context.close();
|
108 | 110 | }
|
109 | 111 |
|
| 112 | + @Test |
| 113 | + void singleSmartLifecycleAutoStartupWithFailingLifecycleBean() { |
| 114 | + CopyOnWriteArrayList<Lifecycle> startedBeans = new CopyOnWriteArrayList<>(); |
| 115 | + TestSmartLifecycleBean bean = TestSmartLifecycleBean.forStartupTests(1, startedBeans); |
| 116 | + bean.setAutoStartup(true); |
| 117 | + StaticApplicationContext context = new StaticApplicationContext(); |
| 118 | + context.getBeanFactory().registerSingleton("bean", bean); |
| 119 | + context.registerSingleton("failingBean", FailingLifecycleBean.class); |
| 120 | + assertThat(bean.isRunning()).isFalse(); |
| 121 | + assertThatExceptionOfType(ApplicationContextException.class) |
| 122 | + .isThrownBy(context::refresh).withCauseInstanceOf(IllegalStateException.class); |
| 123 | + assertThat(bean.isRunning()).isFalse(); |
| 124 | + assertThat(startedBeans).hasSize(1); |
| 125 | + } |
| 126 | + |
110 | 127 | @Test
|
111 | 128 | void singleSmartLifecycleWithoutAutoStartup() {
|
112 | 129 | CopyOnWriteArrayList<Lifecycle> startedBeans = new CopyOnWriteArrayList<>();
|
@@ -832,4 +849,23 @@ public int getPhase() {
|
832 | 849 | }
|
833 | 850 | }
|
834 | 851 |
|
| 852 | + |
| 853 | + public static class FailingLifecycleBean implements SmartLifecycle { |
| 854 | + |
| 855 | + @Override |
| 856 | + public void start() { |
| 857 | + throw new IllegalStateException(); |
| 858 | + } |
| 859 | + |
| 860 | + @Override |
| 861 | + public void stop() { |
| 862 | + throw new IllegalStateException(); |
| 863 | + } |
| 864 | + |
| 865 | + @Override |
| 866 | + public boolean isRunning() { |
| 867 | + return false; |
| 868 | + } |
| 869 | + } |
| 870 | + |
835 | 871 | }
|
0 commit comments