Skip to content

Commit d34ccb3

Browse files
committed
Avoid duplicate test database replacement in native tests
Closes gh-33100
1 parent 954ed3e commit d34ccb3

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.apache.commons.logging.Log;
2525
import org.apache.commons.logging.LogFactory;
2626

27+
import org.springframework.aot.AotDetector;
2728
import org.springframework.beans.BeansException;
2829
import org.springframework.beans.factory.FactoryBean;
2930
import org.springframework.beans.factory.InitializingBean;
@@ -83,6 +84,9 @@ static class EmbeddedDataSourceBeanFactoryPostProcessor implements BeanDefinitio
8384

8485
@Override
8586
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
87+
if (AotDetector.useGeneratedArtifacts()) {
88+
return;
89+
}
8690
Assert.isInstanceOf(ConfigurableListableBeanFactory.class, registry,
8791
"Test Database Auto-configuration can only be used with a ConfigurableListableBeanFactory");
8892
process(registry, (ConfigurableListableBeanFactory) registry);

spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/TestDatabaseAutoConfigurationTests.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.junit.jupiter.api.Test;
2222

2323
import org.springframework.boot.autoconfigure.AutoConfigurations;
24+
import org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration.EmbeddedDataSourceFactoryBean;
2425
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
2526
import org.springframework.context.annotation.Bean;
2627
import org.springframework.context.annotation.Configuration;
@@ -49,6 +50,7 @@ void replaceWithNoDataSourceAvailable() {
4950
@Test
5051
void replaceWithUniqueDatabase() {
5152
this.contextRunner.withUserConfiguration(ExistingDataSourceConfiguration.class).run((context) -> {
53+
assertThat(context).hasSingleBean(EmbeddedDataSourceFactoryBean.class);
5254
DataSource datasource = context.getBean(DataSource.class);
5355
JdbcTemplate jdbcTemplate = new JdbcTemplate(datasource);
5456
jdbcTemplate.execute("create table example (id int, name varchar);");
@@ -60,6 +62,13 @@ void replaceWithUniqueDatabase() {
6062
});
6163
}
6264

65+
@Test
66+
void whenUsingAotGeneratedArtifactsEmbeddedDataSourceFactoryBeanIsNotDefined() {
67+
this.contextRunner.withUserConfiguration(ExistingDataSourceConfiguration.class)
68+
.withSystemProperties("spring.aot.enabled=true")
69+
.run((context) -> assertThat(context).doesNotHaveBean(EmbeddedDataSourceFactoryBean.class));
70+
}
71+
6372
@Configuration(proxyBeanMethods = false)
6473
static class ExistingDataSourceConfiguration {
6574

0 commit comments

Comments
 (0)