Swallow BeanCurrentlyInCreationException exceptions

Update `TestcontainersLifecycleBeanPostProcessor` to that initialization
doesn't fail if a `BeanCurrentlyInCreationException` is thrown.

Prior to this commit, if the first bean being post-processed was a
configuration class declaring a bean that the `Container` depended on
all initialization would fail.

See gh-35223
This commit is contained in:
Phillip Webb
2023-05-02 18:53:31 -07:00
parent 3997771f6c
commit e9578fe745
2 changed files with 60 additions and 8 deletions

View File

@@ -95,6 +95,15 @@ class TestcontainersLifecycleApplicationContextInitializerTests {
assertThat(applicationContext.getBeanFactoryPostProcessors()).hasSize(initialNumberOfPostProcessors + 1);
}
@Test
void dealsWithBeanCurrentlyInCreationException() {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
new TestcontainersLifecycleApplicationContextInitializer().initialize(applicationContext);
applicationContext.register(BeanCurrentlyInCreationExceptionConfiguration2.class,
BeanCurrentlyInCreationExceptionConfiguration1.class);
applicationContext.refresh();
}
private AnnotationConfigApplicationContext createApplicationContext(Startable container) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
new TestcontainersLifecycleApplicationContextInitializer().initialize(applicationContext);
@@ -114,4 +123,33 @@ class TestcontainersLifecycleApplicationContextInitializerTests {
}
@Configuration
static class BeanCurrentlyInCreationExceptionConfiguration1 {
@Bean
TestBean testBean() {
return new TestBean();
}
}
@Configuration
static class BeanCurrentlyInCreationExceptionConfiguration2 {
BeanCurrentlyInCreationExceptionConfiguration2(TestBean testBean) {
}
@Bean
GenericContainer<?> container(TestBean testBean) {
GenericContainer<?> container = mock(GenericContainer.class);
given(container.isShouldBeReused()).willReturn(true);
return container;
}
}
static class TestBean {
}
}