Skip to content

Commit 3e2d151

Browse files
Merge branch '3.1.x' into 3.2.x
Closes gh-40508
2 parents 00a25f5 + f210d83 commit 3e2d151

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.testcontainers.containers.ContainerState;
3030
import org.testcontainers.containers.GenericContainer;
3131
import org.testcontainers.lifecycle.Startable;
32+
import org.testcontainers.utility.TestcontainersConfiguration;
3233

3334
import org.springframework.beans.BeansException;
3435
import org.springframework.beans.factory.BeanCreationException;
@@ -55,6 +56,7 @@
5556
*
5657
* @author Phillip Webb
5758
* @author Stephane Nicoll
59+
* @author Scott Frederick
5860
* @see TestcontainersLifecycleApplicationContextInitializer
5961
*/
6062
@Order(Ordered.LOWEST_PRECEDENCE)
@@ -183,7 +185,8 @@ private boolean isDestroyedByFramework(String beanName) {
183185
}
184186

185187
private boolean isReusedContainer(Object bean) {
186-
return (bean instanceof GenericContainer<?> container) && container.isShouldBeReused();
188+
return (bean instanceof GenericContainer<?> container) && container.isShouldBeReused()
189+
&& TestcontainersConfiguration.getInstance().environmentSupportsReuse();
187190
}
188191

189192
enum Startables {

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

Lines changed: 37 additions & 3 deletions
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.
@@ -18,9 +18,11 @@
1818

1919
import java.util.Map;
2020

21+
import org.junit.jupiter.api.BeforeEach;
2122
import org.junit.jupiter.api.Test;
2223
import org.testcontainers.containers.GenericContainer;
2324
import org.testcontainers.lifecycle.Startable;
25+
import org.testcontainers.utility.TestcontainersConfiguration;
2426

2527
import org.springframework.beans.factory.config.BeanPostProcessor;
2628
import org.springframework.beans.factory.support.AbstractBeanFactory;
@@ -43,9 +45,15 @@
4345
*
4446
* @author Stephane Nicoll
4547
* @author Phillip Webb
48+
* @author Scott Frederick
4649
*/
4750
class TestcontainersLifecycleApplicationContextInitializerTests {
4851

52+
@BeforeEach
53+
void setUp() {
54+
TestcontainersConfiguration.getInstance().updateUserConfig("testcontainers.reuse.enable", "false");
55+
}
56+
4957
@Test
5058
void whenStartableBeanInvokesStartOnRefresh() {
5159
Startable container = mock(Startable.class);
@@ -67,7 +75,8 @@ void whenStartableBeanInvokesCloseOnShutdown() {
6775
}
6876

6977
@Test
70-
void whenReusableContainerBeanInvokesStartButNotClose() {
78+
void whenReusableContainerAndReuseEnabledBeanInvokesStartButNotClose() {
79+
TestcontainersConfiguration.getInstance().updateUserConfig("testcontainers.reuse.enable", "true");
7180
GenericContainer<?> container = mock(GenericContainer.class);
7281
given(container.isShouldBeReused()).willReturn(true);
7382
AnnotationConfigApplicationContext applicationContext = createApplicationContext(container);
@@ -79,7 +88,20 @@ void whenReusableContainerBeanInvokesStartButNotClose() {
7988
}
8089

8190
@Test
82-
void whenReusableContainerBeanFromConfigurationInvokesStartButNotClose() {
91+
void whenReusableContainerButReuseNotEnabledBeanInvokesStartAndClose() {
92+
GenericContainer<?> container = mock(GenericContainer.class);
93+
given(container.isShouldBeReused()).willReturn(true);
94+
AnnotationConfigApplicationContext applicationContext = createApplicationContext(container);
95+
then(container).shouldHaveNoInteractions();
96+
applicationContext.refresh();
97+
then(container).should().start();
98+
applicationContext.close();
99+
then(container).should(times(1)).close();
100+
}
101+
102+
@Test
103+
void whenReusableContainerAndReuseEnabledBeanFromConfigurationInvokesStartButNotClose() {
104+
TestcontainersConfiguration.getInstance().updateUserConfig("testcontainers.reuse.enable", "true");
83105
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
84106
new TestcontainersLifecycleApplicationContextInitializer().initialize(applicationContext);
85107
applicationContext.register(ReusableContainerConfiguration.class);
@@ -90,6 +112,18 @@ void whenReusableContainerBeanFromConfigurationInvokesStartButNotClose() {
90112
then(container).should(never()).close();
91113
}
92114

115+
@Test
116+
void whenReusableContainerButReuseNotEnabledBeanFromConfigurationInvokesStartAndClose() {
117+
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
118+
new TestcontainersLifecycleApplicationContextInitializer().initialize(applicationContext);
119+
applicationContext.register(ReusableContainerConfiguration.class);
120+
applicationContext.refresh();
121+
GenericContainer<?> container = applicationContext.getBean(GenericContainer.class);
122+
then(container).should().start();
123+
applicationContext.close();
124+
then(container).should(times(1)).close();
125+
}
126+
93127
@Test
94128
void doesNotInitializeSameContextMoreThanOnce() {
95129
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();

0 commit comments

Comments
 (0)