Fixup tests to use new ApplicationContextTester
Update existing tests that previously use `ContextLoader` to the newly introduced `*ApplicationContextTester` classes. See gh-9634
This commit is contained in:
@@ -20,7 +20,8 @@ import javax.sql.DataSource;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.test.context.ContextLoader;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.test.context.ApplicationContextTester;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
@@ -37,29 +38,32 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
*/
|
||||
public class TestDatabaseAutoConfigurationTests {
|
||||
|
||||
private final ContextLoader contextLoader = ContextLoader.standard()
|
||||
.autoConfig(TestDatabaseAutoConfiguration.class);
|
||||
private final ApplicationContextTester context = new ApplicationContextTester()
|
||||
.withConfiguration(
|
||||
AutoConfigurations.of(TestDatabaseAutoConfiguration.class));
|
||||
|
||||
@Test
|
||||
public void replaceWithNoDataSourceAvailable() {
|
||||
this.contextLoader.load(context -> {
|
||||
assertThat(context.getBeansOfType(DataSource.class)).isEmpty();
|
||||
});
|
||||
this.context
|
||||
.run((loaded) -> assertThat(loaded).doesNotHaveBean(DataSource.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void replaceWithUniqueDatabase() {
|
||||
this.contextLoader.config(ExistingDataSourceConfiguration.class).load(context -> {
|
||||
DataSource datasource = context.getBean(DataSource.class);
|
||||
JdbcTemplate jdbcTemplate = new JdbcTemplate(datasource);
|
||||
jdbcTemplate.execute("create table example (id int, name varchar);");
|
||||
this.contextLoader.load(anotherContext -> {
|
||||
DataSource anotherDatasource = anotherContext.getBean(DataSource.class);
|
||||
JdbcTemplate anotherJdbcTemplate = new JdbcTemplate(anotherDatasource);
|
||||
anotherJdbcTemplate
|
||||
.execute("create table example (id int, name varchar);");
|
||||
});
|
||||
});
|
||||
this.context.withUserConfiguration(ExistingDataSourceConfiguration.class)
|
||||
.run((loaded) -> {
|
||||
DataSource datasource = loaded.getBean(DataSource.class);
|
||||
JdbcTemplate jdbcTemplate = new JdbcTemplate(datasource);
|
||||
jdbcTemplate.execute("create table example (id int, name varchar);");
|
||||
this.context.run((secondContext) -> {
|
||||
DataSource anotherDatasource = secondContext
|
||||
.getBean(DataSource.class);
|
||||
JdbcTemplate anotherJdbcTemplate = new JdbcTemplate(
|
||||
anotherDatasource);
|
||||
anotherJdbcTemplate
|
||||
.execute("create table example (id int, name varchar);");
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@Configuration
|
||||
|
||||
@@ -22,8 +22,9 @@ import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration;
|
||||
import org.springframework.boot.test.context.ContextLoader;
|
||||
import org.springframework.boot.test.context.ApplicationContextTester;
|
||||
import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions;
|
||||
import org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathRunner;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
@@ -43,31 +44,32 @@ import static org.mockito.Mockito.mock;
|
||||
@ClassPathExclusions({ "h2-*.jar", "hsqldb-*.jar", "derby-*.jar" })
|
||||
public class TestDatabaseAutoConfigurationNoEmbeddedTests {
|
||||
|
||||
private final ContextLoader contextLoader = ContextLoader.standard()
|
||||
.config(ExistingDataSourceConfiguration.class)
|
||||
.autoConfig(TestDatabaseAutoConfiguration.class);
|
||||
private final ApplicationContextTester context = new ApplicationContextTester()
|
||||
.withUserConfiguration(ExistingDataSourceConfiguration.class)
|
||||
.withConfiguration(
|
||||
AutoConfigurations.of(TestDatabaseAutoConfiguration.class));
|
||||
|
||||
@Test
|
||||
public void applyAnyReplace() {
|
||||
this.contextLoader.loadAndFail(BeanCreationException.class, ex -> {
|
||||
String message = ex.getMessage();
|
||||
assertThat(message).contains(
|
||||
"Failed to replace DataSource with an embedded database for tests.");
|
||||
assertThat(message).contains(
|
||||
"If you want an embedded database please put a supported one on the "
|
||||
+ "classpath");
|
||||
assertThat(message).contains(
|
||||
"or tune the replace attribute of @AutoconfigureTestDatabase.");
|
||||
this.context.run((loaded) -> {
|
||||
assertThat(loaded).getFailure().isInstanceOf(BeanCreationException.class)
|
||||
.hasMessageContaining(
|
||||
"Failed to replace DataSource with an embedded database for tests.")
|
||||
.hasMessageContaining(
|
||||
"If you want an embedded database please put a supported one on the classpath")
|
||||
.hasMessageContaining(
|
||||
"or tune the replace attribute of @AutoconfigureTestDatabase.");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void applyNoReplace() {
|
||||
this.contextLoader.env("spring.test.database.replace=NONE").load(context -> {
|
||||
assertThat(context.getBeansOfType(DataSource.class)).hasSize(1);
|
||||
assertThat(context.getBean(DataSource.class))
|
||||
.isSameAs(context.getBean("myCustomDataSource"));
|
||||
});
|
||||
this.context.withPropertyValues("spring.test.database.replace=NONE")
|
||||
.run((loaded) -> {
|
||||
assertThat(loaded).hasSingleBean(DataSource.class);
|
||||
assertThat(loaded).getBean(DataSource.class)
|
||||
.isSameAs(loaded.getBean("myCustomDataSource"));
|
||||
});
|
||||
}
|
||||
|
||||
@Configuration
|
||||
|
||||
Reference in New Issue
Block a user