Skip to content

Commit e7e5ed1

Browse files
committed
Don't initialize containers during AOT processing
Fixes gh-41838
1 parent 1d45016 commit e7e5ed1

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

spring-boot-project/spring-boot-testcontainers/src/main/java/org/springframework/boot/testcontainers/lifecycle/TestcontainersLifecycleBeanPostProcessor.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public void onApplicationEvent(BeforeTestcontainerUsedEvent event) {
8585

8686
@Override
8787
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
88-
if (this.beanFactory.isConfigurationFrozen()) {
88+
if (this.beanFactory.isConfigurationFrozen() && !isAotProcessingInProgress()) {
8989
initializeContainers();
9090
}
9191
if (bean instanceof Startable startableBean) {
@@ -100,6 +100,10 @@ else if (this.startables.get() == Startables.STARTED) {
100100
return bean;
101101
}
102102

103+
private boolean isAotProcessingInProgress() {
104+
return Boolean.getBoolean("spring.aot.processing");
105+
}
106+
103107
private void initializeStartables(Startable startableBean, String startableBeanName) {
104108
logger.trace(LogMessage.format("Initializing startables"));
105109
List<String> beanNames = new ArrayList<>(getBeanNames(Startable.class));

spring-boot-project/spring-boot-testcontainers/src/test/java/org/springframework/boot/testcontainers/lifecycle/TestcontainersLifecycleApplicationContextInitializerTests.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.testcontainers.lifecycle.Startable;
2525
import org.testcontainers.utility.TestcontainersConfiguration;
2626

27+
import org.springframework.aot.hint.RuntimeHints;
2728
import org.springframework.beans.factory.config.BeanPostProcessor;
2829
import org.springframework.beans.factory.support.AbstractBeanFactory;
2930
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@@ -143,6 +144,17 @@ void dealsWithBeanCurrentlyInCreationException() {
143144
applicationContext.refresh();
144145
}
145146

147+
@Test
148+
void doesNotStartContainersWhenAotProcessingIsInProgress() {
149+
GenericContainer<?> container = mock(GenericContainer.class);
150+
AnnotationConfigApplicationContext applicationContext = createApplicationContext(container);
151+
then(container).shouldHaveNoInteractions();
152+
withSystemProperty("spring.aot.processing", "true",
153+
() -> applicationContext.refreshForAotProcessing(new RuntimeHints()));
154+
then(container).shouldHaveNoInteractions();
155+
applicationContext.close();
156+
}
157+
146158
@Test
147159
void setupStartupBasedOnEnvironmentProperty() {
148160
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
@@ -159,13 +171,36 @@ void setupStartupBasedOnEnvironmentProperty() {
159171
assertThat(beanPostProcessor).extracting("startup").isEqualTo(TestcontainersStartup.PARALLEL);
160172
}
161173

174+
private void withSystemProperty(String name, String value, Runnable action) {
175+
String previousValue = System.getProperty(name);
176+
System.setProperty(name, value);
177+
try {
178+
action.run();
179+
}
180+
finally {
181+
if (previousValue == null) {
182+
System.clearProperty(name);
183+
}
184+
else {
185+
System.setProperty(name, previousValue);
186+
}
187+
}
188+
}
189+
162190
private AnnotationConfigApplicationContext createApplicationContext(Startable container) {
163191
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
164192
new TestcontainersLifecycleApplicationContextInitializer().initialize(applicationContext);
165193
applicationContext.registerBean("container", Startable.class, () -> container);
166194
return applicationContext;
167195
}
168196

197+
private AnnotationConfigApplicationContext createApplicationContext(GenericContainer<?> container) {
198+
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
199+
new TestcontainersLifecycleApplicationContextInitializer().initialize(applicationContext);
200+
applicationContext.registerBean("container", GenericContainer.class, () -> container);
201+
return applicationContext;
202+
}
203+
169204
@Configuration
170205
static class ReusableContainerConfiguration {
171206

0 commit comments

Comments
 (0)