Avoid duplicate test database replacement in native tests

Closes gh-33100
pull/33108/head
Andy Wilkinson 2 years ago
parent 954ed3e15e
commit d34ccb3880

@ -24,6 +24,7 @@ import javax.sql.DataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aot.AotDetector;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
@ -83,6 +84,9 @@ public class TestDatabaseAutoConfiguration {
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
if (AotDetector.useGeneratedArtifacts()) {
return;
}
Assert.isInstanceOf(ConfigurableListableBeanFactory.class, registry,
"Test Database Auto-configuration can only be used with a ConfigurableListableBeanFactory");
process(registry, (ConfigurableListableBeanFactory) registry);

@ -21,6 +21,7 @@ import javax.sql.DataSource;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration.EmbeddedDataSourceFactoryBean;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@ -49,6 +50,7 @@ class TestDatabaseAutoConfigurationTests {
@Test
void replaceWithUniqueDatabase() {
this.contextRunner.withUserConfiguration(ExistingDataSourceConfiguration.class).run((context) -> {
assertThat(context).hasSingleBean(EmbeddedDataSourceFactoryBean.class);
DataSource datasource = context.getBean(DataSource.class);
JdbcTemplate jdbcTemplate = new JdbcTemplate(datasource);
jdbcTemplate.execute("create table example (id int, name varchar);");
@ -60,6 +62,13 @@ class TestDatabaseAutoConfigurationTests {
});
}
@Test
void whenUsingAotGeneratedArtifactsEmbeddedDataSourceFactoryBeanIsNotDefined() {
this.contextRunner.withUserConfiguration(ExistingDataSourceConfiguration.class)
.withSystemProperties("spring.aot.enabled=true")
.run((context) -> assertThat(context).doesNotHaveBean(EmbeddedDataSourceFactoryBean.class));
}
@Configuration(proxyBeanMethods = false)
static class ExistingDataSourceConfiguration {

Loading…
Cancel
Save