pull/17660/head
Stephane Nicoll 5 years ago
parent 2c3d4501ed
commit 97907aac30

@ -16,6 +16,8 @@
package org.springframework.boot.autoconfigure.data.jdbc; package org.springframework.boot.autoconfigure.data.jdbc;
import java.util.function.Function;
import javax.sql.DataSource; import javax.sql.DataSource;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -27,7 +29,6 @@ import org.springframework.boot.autoconfigure.data.jdbc.city.City;
import org.springframework.boot.autoconfigure.data.jdbc.city.CityRepository; import org.springframework.boot.autoconfigure.data.jdbc.city.CityRepository;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
import org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration;
import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@ -42,6 +43,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* Tests for {@link JdbcRepositoriesAutoConfiguration}. * Tests for {@link JdbcRepositoriesAutoConfiguration}.
* *
* @author Andy Wilkinson * @author Andy Wilkinson
* @author Stephane Nicoll
*/ */
class JdbcRepositoriesAutoConfigurationTests { class JdbcRepositoriesAutoConfigurationTests {
@ -56,22 +58,18 @@ class JdbcRepositoriesAutoConfigurationTests {
@Test @Test
void backsOffWithNoJdbcOperations() { void backsOffWithNoJdbcOperations() {
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class, TestConfiguration.class) this.contextRunner.with(database()).withUserConfiguration(TestConfiguration.class).run((context) -> {
.run((context) -> { assertThat(context).hasSingleBean(DataSource.class);
assertThat(context).hasSingleBean(DataSource.class); assertThat(context).doesNotHaveBean(JdbcRepositoryConfigExtension.class);
assertThat(context).doesNotHaveBean(JdbcRepositoryConfigExtension.class); });
});
} }
@Test @Test
void basicAutoConfiguration() { void basicAutoConfiguration() {
this.contextRunner this.contextRunner.with(database())
.withConfiguration(AutoConfigurations.of(JdbcTemplateAutoConfiguration.class, .withConfiguration(AutoConfigurations.of(JdbcTemplateAutoConfiguration.class,
DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class)) DataSourceTransactionManagerAutoConfiguration.class))
.withUserConfiguration(TestConfiguration.class, EmbeddedDataSourceConfiguration.class) .withUserConfiguration(TestConfiguration.class).run((context) -> {
.withPropertyValues("spring.datasource.schema=classpath:data-jdbc-schema.sql",
"spring.datasource.data=classpath:city.sql", "spring.datasource.generate-unique-name:true")
.run((context) -> {
assertThat(context).hasSingleBean(AbstractJdbcConfiguration.class); assertThat(context).hasSingleBean(AbstractJdbcConfiguration.class);
assertThat(context).hasSingleBean(CityRepository.class); assertThat(context).hasSingleBean(CityRepository.class);
assertThat(context.getBean(CityRepository.class).findById(2000L)).isPresent(); assertThat(context.getBean(CityRepository.class).findById(2000L)).isPresent();
@ -80,9 +78,10 @@ class JdbcRepositoriesAutoConfigurationTests {
@Test @Test
void autoConfigurationWithNoRepositories() { void autoConfigurationWithNoRepositories() {
this.contextRunner.withConfiguration(AutoConfigurations.of(JdbcTemplateAutoConfiguration.class)) this.contextRunner.with(database())
.withUserConfiguration(EmbeddedDataSourceConfiguration.class, EmptyConfiguration.class) .withConfiguration(AutoConfigurations.of(JdbcTemplateAutoConfiguration.class,
.run((context) -> { DataSourceTransactionManagerAutoConfiguration.class))
.withUserConfiguration(EmptyConfiguration.class).run((context) -> {
assertThat(context).hasSingleBean(AbstractJdbcConfiguration.class); assertThat(context).hasSingleBean(AbstractJdbcConfiguration.class);
assertThat(context).doesNotHaveBean(Repository.class); assertThat(context).doesNotHaveBean(Repository.class);
}); });
@ -90,19 +89,22 @@ class JdbcRepositoriesAutoConfigurationTests {
@Test @Test
void honoursUsersEnableJdbcRepositoriesConfiguration() { void honoursUsersEnableJdbcRepositoriesConfiguration() {
this.contextRunner this.contextRunner.with(database())
.withConfiguration(AutoConfigurations.of(JdbcTemplateAutoConfiguration.class, .withConfiguration(AutoConfigurations.of(JdbcTemplateAutoConfiguration.class,
DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class)) DataSourceTransactionManagerAutoConfiguration.class))
.withUserConfiguration(EnableRepositoriesConfiguration.class, EmbeddedDataSourceConfiguration.class) .withUserConfiguration(EnableRepositoriesConfiguration.class).run((context) -> {
.withPropertyValues("spring.datasource.schema=classpath:data-jdbc-schema.sql",
"spring.datasource.data=classpath:city.sql", "spring.datasource.generate-unique-name:true")
.run((context) -> {
assertThat(context).hasSingleBean(AbstractJdbcConfiguration.class); assertThat(context).hasSingleBean(AbstractJdbcConfiguration.class);
assertThat(context).hasSingleBean(CityRepository.class); assertThat(context).hasSingleBean(CityRepository.class);
assertThat(context.getBean(CityRepository.class).findById(2000L)).isPresent(); assertThat(context.getBean(CityRepository.class).findById(2000L)).isPresent();
}); });
} }
private Function<ApplicationContextRunner, ApplicationContextRunner> database() {
return (runner) -> runner.withConfiguration(AutoConfigurations.of(DataSourceAutoConfiguration.class))
.withPropertyValues("spring.datasource.schema=classpath:data-jdbc-schema.sql",
"spring.datasource.data=classpath:city.sql", "spring.datasource.generate-unique-name:true");
}
@TestAutoConfigurationPackage(City.class) @TestAutoConfigurationPackage(City.class)
static class TestConfiguration { static class TestConfiguration {

Loading…
Cancel
Save