Skip to content

Commit 8cc0d55

Browse files
committed
Avoid unwanted bean overrides in spring-boot-test-autoconfigure
See gh-13609
1 parent cb8e5e5 commit 8cc0d55

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
lines changed

Diff for: spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/jdbc/TestDatabaseAutoConfiguration.java

+1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ private void process(BeanDefinitionRegistry registry,
105105
boolean primary = holder.getBeanDefinition().isPrimary();
106106
logger.info("Replacing '" + beanName + "' DataSource bean with "
107107
+ (primary ? "primary " : "") + "embedded version");
108+
registry.removeBeanDefinition(beanName);
108109
registry.registerBeanDefinition(beanName,
109110
createEmbeddedBeanDefinition(primary));
110111
}
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.boot.test.autoconfigure;
17+
package org.springframework.boot.test.autoconfigure.override;
1818

1919
import org.junit.Rule;
2020
import org.junit.Test;
@@ -25,6 +25,8 @@
2525
import org.springframework.beans.factory.annotation.Autowired;
2626
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
2727
import org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor;
28+
import org.springframework.boot.test.autoconfigure.ExampleTestConfig;
29+
import org.springframework.boot.test.autoconfigure.OverrideAutoConfiguration;
2830
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
2931
import org.springframework.context.ApplicationContext;
3032
import org.springframework.test.context.BootstrapWith;
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.boot.test.autoconfigure;
17+
package org.springframework.boot.test.autoconfigure.override;
1818

1919
import org.junit.Test;
2020
import org.junit.runner.RunWith;
2121

2222
import org.springframework.beans.factory.annotation.Autowired;
2323
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
2424
import org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor;
25+
import org.springframework.boot.test.autoconfigure.ExampleTestConfig;
26+
import org.springframework.boot.test.autoconfigure.OverrideAutoConfiguration;
2527
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
2628
import org.springframework.context.ApplicationContext;
2729
import org.springframework.test.context.BootstrapWith;
@@ -47,7 +49,8 @@ public class OverrideAutoConfigurationEnabledTrueIntegrationTests {
4749
@Test
4850
public void autoConfiguredContext() {
4951
ApplicationContext context = this.context;
50-
assertThat(context.getBean(ExampleSpringBootApplication.class)).isNotNull();
52+
assertThat(context.getBean(OverrideAutoConfigurationSpringBootApplication.class))
53+
.isNotNull();
5154
assertThat(context.getBean(ConfigurationPropertiesBindingPostProcessor.class))
5255
.isNotNull();
5356
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2012-2018 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+
* http://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.test.autoconfigure.override;
18+
19+
import org.springframework.boot.SpringBootConfiguration;
20+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
21+
import org.springframework.boot.autoconfigure.SpringBootApplication;
22+
import org.springframework.boot.test.autoconfigure.OverrideAutoConfiguration;
23+
24+
/**
25+
* Example {@link SpringBootApplication @SpringBootApplication} for use with
26+
* {@link OverrideAutoConfiguration} tests.
27+
*
28+
* @author Andy Wilkinson
29+
*/
30+
@SpringBootConfiguration
31+
@EnableAutoConfiguration
32+
public class OverrideAutoConfigurationSpringBootApplication {
33+
34+
}

0 commit comments

Comments
 (0)