Skip to content

Commit 4d4b189

Browse files
committed
Prevent duplicate DynamicPropertyRegistry beans
Remove the Spring Framework registered `DynamicPropertyRegistry` when using Testcontainers. See gh-41839
1 parent c3ed545 commit 4d4b189

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.testcontainers.properties;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.boot.SpringBootConfiguration;
22+
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
23+
import org.springframework.boot.test.context.SpringBootTest;
24+
import org.springframework.boot.test.context.TestConfiguration;
25+
import org.springframework.boot.testcontainers.properties.TestcontainersPropertySourceAutoConfigurationWithSpringBootTestIntegrationTest.TestConfig;
26+
import org.springframework.context.annotation.Bean;
27+
import org.springframework.test.context.DynamicPropertyRegistry;
28+
29+
/**
30+
* Tests for {@link TestcontainersPropertySourceAutoConfiguration} when combined with
31+
* {@link SpringBootTest @SpringBootTest}.
32+
*
33+
* @author Phillip Webb
34+
*/
35+
@SpringBootTest(classes = TestConfig.class)
36+
class TestcontainersPropertySourceAutoConfigurationWithSpringBootTestIntegrationTest {
37+
38+
@Test
39+
void injectsRegistry() {
40+
41+
}
42+
43+
@TestConfiguration
44+
@ImportAutoConfiguration(TestcontainersPropertySourceAutoConfiguration.class)
45+
@SpringBootConfiguration
46+
static class TestConfig {
47+
48+
@Bean
49+
String example(DynamicPropertyRegistry registry) {
50+
registry.add("test", () -> "test");
51+
return "Hello";
52+
}
53+
54+
}
55+
56+
}

spring-boot-project/spring-boot-testcontainers/src/main/java/org/springframework/boot/testcontainers/properties/TestcontainersPropertySourceAutoConfiguration.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

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

19+
import org.springframework.beans.BeansException;
20+
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
21+
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
22+
import org.springframework.beans.factory.support.DefaultSingletonBeanRegistry;
1923
import org.springframework.boot.autoconfigure.AutoConfiguration;
2024
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2125
import org.springframework.context.ConfigurableApplicationContext;
@@ -39,9 +43,27 @@ public class TestcontainersPropertySourceAutoConfiguration {
3943
TestcontainersPropertySourceAutoConfiguration() {
4044
}
4145

46+
@Bean
47+
static RemoveTestDynamicPropertyRegistryBeanPostProcessor removeTestDynamicPropertyRegistryBeanPostProcessor() {
48+
return new RemoveTestDynamicPropertyRegistryBeanPostProcessor();
49+
}
50+
4251
@Bean
4352
static DynamicPropertyRegistry dynamicPropertyRegistry(ConfigurableApplicationContext applicationContext) {
4453
return TestcontainersPropertySource.attach(applicationContext);
4554
}
4655

56+
static class RemoveTestDynamicPropertyRegistryBeanPostProcessor implements BeanFactoryPostProcessor {
57+
58+
@Override
59+
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
60+
if (beanFactory instanceof DefaultSingletonBeanRegistry singletonBeanRegistry) {
61+
singletonBeanRegistry
62+
.destroySingleton("org.springframework.test.context.support.DynamicPropertiesContextCustomizer"
63+
+ ".dynamicPropertyRegistry");
64+
}
65+
}
66+
67+
}
68+
4769
}

0 commit comments

Comments
 (0)