Skip to content

Commit f210d83

Browse files
Don't reuse Testcontainers containers if reuse is globally disabled
Fixes gh-39609
1 parent 178d3ca commit f210d83

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,6 +25,7 @@
2525
import org.testcontainers.containers.ContainerState;
2626
import org.testcontainers.containers.GenericContainer;
2727
import org.testcontainers.lifecycle.Startable;
28+
import org.testcontainers.utility.TestcontainersConfiguration;
2829

2930
import org.springframework.beans.BeansException;
3031
import org.springframework.beans.factory.BeanCreationException;
@@ -49,6 +50,7 @@
4950
*
5051
* @author Phillip Webb
5152
* @author Stephane Nicoll
53+
* @author Scott Frederick
5254
* @see TestcontainersLifecycleApplicationContextInitializer
5355
*/
5456
@Order(Ordered.LOWEST_PRECEDENCE)
@@ -124,7 +126,8 @@ private boolean isDestroyedByFramework(String beanName) {
124126
}
125127

126128
private boolean isReusedContainer(Object bean) {
127-
return (bean instanceof GenericContainer<?> container) && container.isShouldBeReused();
129+
return (bean instanceof GenericContainer<?> container) && container.isShouldBeReused()
130+
&& TestcontainersConfiguration.getInstance().environmentSupportsReuse();
128131
}
129132

130133
}

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

+37-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,9 +16,11 @@
1616

1717
package org.springframework.boot.testcontainers.lifecycle;
1818

19+
import org.junit.jupiter.api.BeforeEach;
1920
import org.junit.jupiter.api.Test;
2021
import org.testcontainers.containers.GenericContainer;
2122
import org.testcontainers.lifecycle.Startable;
23+
import org.testcontainers.utility.TestcontainersConfiguration;
2224

2325
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
2426
import org.springframework.context.annotation.Bean;
@@ -38,9 +40,15 @@
3840
*
3941
* @author Stephane Nicoll
4042
* @author Phillip Webb
43+
* @author Scott Frederick
4144
*/
4245
class TestcontainersLifecycleApplicationContextInitializerTests {
4346

47+
@BeforeEach
48+
void setUp() {
49+
TestcontainersConfiguration.getInstance().updateUserConfig("testcontainers.reuse.enable", "false");
50+
}
51+
4452
@Test
4553
void whenStartableBeanInvokesStartOnRefresh() {
4654
Startable container = mock(Startable.class);
@@ -62,7 +70,8 @@ void whenStartableBeanInvokesCloseOnShutdown() {
6270
}
6371

6472
@Test
65-
void whenReusableContainerBeanInvokesStartButNotClose() {
73+
void whenReusableContainerAndReuseEnabledBeanInvokesStartButNotClose() {
74+
TestcontainersConfiguration.getInstance().updateUserConfig("testcontainers.reuse.enable", "true");
6675
GenericContainer<?> container = mock(GenericContainer.class);
6776
given(container.isShouldBeReused()).willReturn(true);
6877
AnnotationConfigApplicationContext applicationContext = createApplicationContext(container);
@@ -74,7 +83,20 @@ void whenReusableContainerBeanInvokesStartButNotClose() {
7483
}
7584

7685
@Test
77-
void whenReusableContainerBeanFromConfigurationInvokesStartButNotClose() {
86+
void whenReusableContainerButReuseNotEnabledBeanInvokesStartAndClose() {
87+
GenericContainer<?> container = mock(GenericContainer.class);
88+
given(container.isShouldBeReused()).willReturn(true);
89+
AnnotationConfigApplicationContext applicationContext = createApplicationContext(container);
90+
then(container).shouldHaveNoInteractions();
91+
applicationContext.refresh();
92+
then(container).should().start();
93+
applicationContext.close();
94+
then(container).should(times(1)).close();
95+
}
96+
97+
@Test
98+
void whenReusableContainerAndReuseEnabledBeanFromConfigurationInvokesStartButNotClose() {
99+
TestcontainersConfiguration.getInstance().updateUserConfig("testcontainers.reuse.enable", "true");
78100
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
79101
new TestcontainersLifecycleApplicationContextInitializer().initialize(applicationContext);
80102
applicationContext.register(ReusableContainerConfiguration.class);
@@ -85,6 +107,18 @@ void whenReusableContainerBeanFromConfigurationInvokesStartButNotClose() {
85107
then(container).should(never()).close();
86108
}
87109

110+
@Test
111+
void whenReusableContainerButReuseNotEnabledBeanFromConfigurationInvokesStartAndClose() {
112+
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
113+
new TestcontainersLifecycleApplicationContextInitializer().initialize(applicationContext);
114+
applicationContext.register(ReusableContainerConfiguration.class);
115+
applicationContext.refresh();
116+
GenericContainer<?> container = applicationContext.getBean(GenericContainer.class);
117+
then(container).should().start();
118+
applicationContext.close();
119+
then(container).should(times(1)).close();
120+
}
121+
88122
@Test
89123
void doesNotInitializeSameContextMoreThanOnce() {
90124
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();

0 commit comments

Comments
 (0)