From b618c70e52d9c102743498d66f5b8b422684b649 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Wed, 23 Nov 2016 14:51:45 +0100 Subject: [PATCH] Add test slice for pure jdbc tests This commit adds `@JdbcTest`, a new test slice for pure jdbc tests. It provides the same basic features than `@DataJpaTest`. Closes gh-6563 --- .../main/asciidoc/spring-boot-features.adoc | 38 ++++++- ...pleTestApplicationWebIntegrationTests.java | 2 +- ...UserVehicleControllerApplicationTests.java | 2 +- .../autoconfigure/jdbc/AutoConfigureJdbc.java | 41 ++++++++ .../jdbc/AutoConfigureTestDatabase.java | 82 ++++++++++++++++ .../test/autoconfigure/jdbc/JdbcTest.java | 98 +++++++++++++++++++ .../jdbc/JdbcTypeExcludeFilter.java | 76 ++++++++++++++ .../TestDatabaseAutoConfiguration.java | 2 +- .../orm/jpa/AutoConfigureTestDatabase.java | 6 +- .../autoconfigure/orm/jpa/DataJpaTest.java | 1 + .../main/resources/META-INF/spring.factories | 16 ++- ...thMultipleDatasourcesIntegrationTests.java | 18 +--- ...atabaseWithNoDatabaseIntegrationTests.java | 2 +- .../autoconfigure/jdbc/ExampleEntity.java | 51 ++++++++++ .../jdbc/ExampleJdbcApplication.java | 41 ++++++++ .../autoconfigure/jdbc/ExampleRepository.java | 70 +++++++++++++ .../jdbc/JdbcTestIntegrationTests.java | 98 +++++++++++++++++++ ...ReplaceAutoConfiguredIntegrationTests.java | 59 +++++++++++ ...iguredWithoutOverrideIntegrationTests.java | 51 ++++++++++ ...tabaseReplaceExplicitIntegrationTests.java | 70 +++++++++++++ ...stDatabaseReplaceNoneIntegrationTests.java | 51 ++++++++++ ...TestWithIncludeFilterIntegrationTests.java | 51 ++++++++++ .../TestDatabaseAutoConfigurationTests.java | 2 +- ...ReplaceAutoConfiguredIntegrationTests.java | 1 + ...iguredWithoutOverrideIntegrationTests.java | 1 + ...tabaseReplaceExplicitIntegrationTests.java | 1 + ...stDatabaseReplaceNoneIntegrationTests.java | 1 + .../boot/test/autoconfigure/jdbc/schema.sql | 1 + 28 files changed, 908 insertions(+), 25 deletions(-) create mode 100644 spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/jdbc/AutoConfigureJdbc.java create mode 100644 spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/jdbc/AutoConfigureTestDatabase.java create mode 100644 spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/jdbc/JdbcTest.java create mode 100644 spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/jdbc/JdbcTypeExcludeFilter.java rename spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/{orm/jpa => jdbc}/TestDatabaseAutoConfiguration.java (99%) rename spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/{orm/jpa => jdbc}/AutoConfigureTestDatabaseWithMultipleDatasourcesIntegrationTests.java (84%) rename spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/{orm/jpa => jdbc}/AutoConfigureTestDatabaseWithNoDatabaseIntegrationTests.java (96%) create mode 100644 spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/ExampleEntity.java create mode 100644 spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/ExampleJdbcApplication.java create mode 100644 spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/ExampleRepository.java create mode 100644 spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/JdbcTestIntegrationTests.java create mode 100644 spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/JdbcTestWithAutoConfigureTestDatabaseReplaceAutoConfiguredIntegrationTests.java create mode 100644 spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/JdbcTestWithAutoConfigureTestDatabaseReplaceAutoConfiguredWithoutOverrideIntegrationTests.java create mode 100644 spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/JdbcTestWithAutoConfigureTestDatabaseReplaceExplicitIntegrationTests.java create mode 100644 spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/JdbcTestWithAutoConfigureTestDatabaseReplaceNoneIntegrationTests.java create mode 100644 spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/JdbcTestWithIncludeFilterIntegrationTests.java rename spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/{orm/jpa => jdbc}/TestDatabaseAutoConfigurationTests.java (98%) create mode 100644 spring-boot-test-autoconfigure/src/test/resources/org/springframework/boot/test/autoconfigure/jdbc/schema.sql diff --git a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc index 45a3ee3e20..a9fac2aebc 100644 --- a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc +++ b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc @@ -5324,7 +5324,7 @@ Data JPA repositories. Regular `@Component` beans will not be loaded into the `ApplicationContext`. Data JPA tests are transactional and rollback at the end of each test by default, -see the {spring-reference}#testcontext-tx-enabling-transactions [relevant section] in the +see the {spring-reference}#testcontext-tx-enabling-transactions[relevant section] in the Spring Reference Documentation for more details. If that's not what you want, you can disable transaction management for a test or for the whole class as follows: @@ -5402,6 +5402,42 @@ A list of the auto-configuration that is enabled by `@DataJpaTest` can be +[[boot-features-testing-spring-boot-applications-testing-autoconfigured-jdbc-test]] +==== Auto-configured JDBC tests +`@JdbcTest` is similar to `@DataJpaTest` but for pure jdbc-related tests. By default it +will also configure an in-memory embedded database and a `JdbcTemplate`. Regular +`@Component` beans will not be loaded into the `ApplicationContext`. + +JDBC tests are transactional and rollback at the end of each test by default, +see the {spring-reference}#testcontext-tx-enabling-transactions[relevant section] in the +Spring Reference Documentation for more details. If that's not what you want, you can +disable transaction management for a test or for the whole class as follows: + +[source,java,indent=0] +---- + import org.junit.Test; + import org.junit.runner.RunWith; + import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest; + import org.springframework.test.context.junit4.SpringRunner; + import org.springframework.transaction.annotation.Propagation; + import org.springframework.transaction.annotation.Transactional; + + @RunWith(SpringRunner.class) + @JdbcTest + @Transactional(propagation = Propagation.NOT_SUPPORTED) + public class ExampleNonTransactionalTests { + + } +---- + +If you prefer your test to run against a real database, you can use the +`@AutoConfigureTestDatabase` annotation the same way as for `DataJpaTest`. + +A list of the auto-configuration that is enabled by `@JdbcTest` can be +<>. + + + [[boot-features-testing-spring-boot-applications-testing-autoconfigured-rest-client]] ==== Auto-configured REST clients The `@RestClientTest` annotation can be used if you want to test REST clients. By default diff --git a/spring-boot-samples/spring-boot-sample-test/src/test/java/sample/test/SampleTestApplicationWebIntegrationTests.java b/spring-boot-samples/spring-boot-sample-test/src/test/java/sample/test/SampleTestApplicationWebIntegrationTests.java index 96710ed94e..9c191d30bd 100644 --- a/spring-boot-samples/spring-boot-sample-test/src/test/java/sample/test/SampleTestApplicationWebIntegrationTests.java +++ b/spring-boot-samples/spring-boot-sample-test/src/test/java/sample/test/SampleTestApplicationWebIntegrationTests.java @@ -24,7 +24,7 @@ import sample.test.service.VehicleDetails; import sample.test.service.VehicleDetailsService; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.orm.jpa.AutoConfigureTestDatabase; +import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.mock.mockito.MockBean; diff --git a/spring-boot-samples/spring-boot-sample-test/src/test/java/sample/test/web/UserVehicleControllerApplicationTests.java b/spring-boot-samples/spring-boot-sample-test/src/test/java/sample/test/web/UserVehicleControllerApplicationTests.java index ec562143f0..6be22763d8 100644 --- a/spring-boot-samples/spring-boot-sample-test/src/test/java/sample/test/web/UserVehicleControllerApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-test/src/test/java/sample/test/web/UserVehicleControllerApplicationTests.java @@ -22,7 +22,7 @@ import sample.test.WelcomeCommandLineRunner; import sample.test.service.VehicleDetails; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.orm.jpa.AutoConfigureTestDatabase; +import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.MockBean; diff --git a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/jdbc/AutoConfigureJdbc.java b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/jdbc/AutoConfigureJdbc.java new file mode 100644 index 0000000000..317afaac29 --- /dev/null +++ b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/jdbc/AutoConfigureJdbc.java @@ -0,0 +1,41 @@ +/* + * Copyright 2012-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.autoconfigure.jdbc; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; + +/** + * {@link ImportAutoConfiguration Auto-configuration imports} for typical jdbc tests. + * Most tests should consider using {@link JdbcTest @JdbcTest} rather than using + * this annotation directly. + * + * @author Stephane Nicoll + * @since 1.5.0 + * @see JdbcTest + */ +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@ImportAutoConfiguration +public @interface AutoConfigureJdbc { +} diff --git a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/jdbc/AutoConfigureTestDatabase.java b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/jdbc/AutoConfigureTestDatabase.java new file mode 100644 index 0000000000..5c454c4ae8 --- /dev/null +++ b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/jdbc/AutoConfigureTestDatabase.java @@ -0,0 +1,82 @@ +/* + * Copyright 2012-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.autoconfigure.jdbc; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import javax.sql.DataSource; + +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; +import org.springframework.boot.autoconfigure.jdbc.EmbeddedDatabaseConnection; +import org.springframework.boot.test.autoconfigure.properties.PropertyMapping; + +/** + * Annotation that can be applied to a test class to configure a test database to use + * instead of any application defined or auto-configured {@link DataSource}. + * + * @author Phillip Webb + * @see TestDatabaseAutoConfiguration + */ +@Target({ ElementType.TYPE, ElementType.METHOD }) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Inherited +@ImportAutoConfiguration +@PropertyMapping("spring.test.database") +public @interface AutoConfigureTestDatabase { + + /** + * Determines what type of existing DataSource beans can be replaced. + * @return the type of existing DataSource to replace + */ + Replace replace() default Replace.ANY; + + /** + * The type of connection to be established when {@link #replace() replacing} the data + * source. By default will attempt to detect the connection based on the classpath. + * @return the type of connection to use + */ + EmbeddedDatabaseConnection connection() default EmbeddedDatabaseConnection.NONE; + + /** + * What the test database can replace. + */ + enum Replace { + + /** + * Replace any DataSource bean (auto-configured or manually defined). + */ + ANY, + + /** + * Only replace auto-configured DataSource. + */ + AUTO_CONFIGURED, + + /** + * Don't replace the application default DataSource. + */ + NONE + + } + +} diff --git a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/jdbc/JdbcTest.java b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/jdbc/JdbcTest.java new file mode 100644 index 0000000000..232bb31b44 --- /dev/null +++ b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/jdbc/JdbcTest.java @@ -0,0 +1,98 @@ +/* + * Copyright 2012-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.autoconfigure.jdbc; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.test.autoconfigure.OverrideAutoConfiguration; +import org.springframework.boot.test.autoconfigure.core.AutoConfigureCache; +import org.springframework.boot.test.autoconfigure.filter.TypeExcludeFilters; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTestContextBootstrapper; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.test.context.BootstrapWith; +import org.springframework.transaction.annotation.Transactional; + +/** + * Annotation that can be used in combination with {@code @RunWith(SpringRunner.class)} + * for a typical jdbc test. Can be used when a test focuses only on + * jdbc-based components. + *

+ * Using this annotation will disable full auto-configuration and instead apply only + * configuration relevant to jdbc tests. + *

+ * By default, tests annotated with {@code @JdbcTest} will use an embedded in-memory + * database (replacing any explicit or usually auto-configured DataSource). The + * {@link AutoConfigureTestDatabase @AutoConfigureTestDatabase} annotation can be used to + * override these settings. + *

+ * If you are looking to load your full application configuration, but use an embedded + * database, you should consider {@link SpringBootTest @SpringBootTest} combined with + * {@link AutoConfigureTestDatabase @AutoConfigureTestDatabase} rather than this + * annotation. + * + * @author Stephane Nicoll + * @see AutoConfigureJdbc + * @see AutoConfigureTestDatabase + * @see AutoConfigureCache + */ +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Inherited +@BootstrapWith(SpringBootTestContextBootstrapper.class) +@OverrideAutoConfiguration(enabled = false) +@TypeExcludeFilters(JdbcTypeExcludeFilter.class) +@Transactional +@AutoConfigureCache +@AutoConfigureJdbc +@AutoConfigureTestDatabase +@ImportAutoConfiguration +public @interface JdbcTest { + + /** + * Determines if default filtering should be used with + * {@link SpringBootApplication @SpringBootApplication}. By default no beans are + * included. + * @see #includeFilters() + * @see #excludeFilters() + * @return if default filters should be used + */ + boolean useDefaultFilters() default true; + + /** + * A set of include filters which can be used to add otherwise filtered beans to the + * application context. + * @return include filters to apply + */ + ComponentScan.Filter[] includeFilters() default {}; + + /** + * A set of exclude filters which can be used to filter beans that would otherwise be + * added to the application context. + * @return exclude filters to apply + */ + ComponentScan.Filter[] excludeFilters() default {}; + +} diff --git a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/jdbc/JdbcTypeExcludeFilter.java b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/jdbc/JdbcTypeExcludeFilter.java new file mode 100644 index 0000000000..9effe0d700 --- /dev/null +++ b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/jdbc/JdbcTypeExcludeFilter.java @@ -0,0 +1,76 @@ +/* + * Copyright 2012-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.autoconfigure.jdbc; + +import java.io.IOException; +import java.util.Collections; +import java.util.Set; + +import org.springframework.boot.context.TypeExcludeFilter; +import org.springframework.boot.test.autoconfigure.filter.AnnotationCustomizableTypeExcludeFilter; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.core.annotation.AnnotatedElementUtils; +import org.springframework.core.type.classreading.MetadataReader; +import org.springframework.core.type.classreading.MetadataReaderFactory; + +/** + * {@link TypeExcludeFilter} for {@link JdbcTest @Jdbctest}. + * + * @author Stephane Nicoll + */ +class JdbcTypeExcludeFilter extends AnnotationCustomizableTypeExcludeFilter { + + private final JdbcTest annotation; + + JdbcTypeExcludeFilter(Class testClass) { + this.annotation = AnnotatedElementUtils.getMergedAnnotation(testClass, + JdbcTest.class); + } + + @Override + protected boolean hasAnnotation() { + return this.annotation != null; + } + + @Override + protected ComponentScan.Filter[] getFilters(FilterType type) { + switch (type) { + case INCLUDE: + return this.annotation.includeFilters(); + case EXCLUDE: + return this.annotation.excludeFilters(); + } + throw new IllegalStateException("Unsupported type " + type); + } + + @Override + protected boolean isUseDefaultFilters() { + return this.annotation.useDefaultFilters(); + } + + @Override + protected boolean defaultInclude(MetadataReader metadataReader, + MetadataReaderFactory metadataReaderFactory) throws IOException { + return false; + } + + @Override + protected Set> getDefaultIncludes() { + return Collections.emptySet(); + } + +} diff --git a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/orm/jpa/TestDatabaseAutoConfiguration.java b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/jdbc/TestDatabaseAutoConfiguration.java similarity index 99% rename from spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/orm/jpa/TestDatabaseAutoConfiguration.java rename to spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/jdbc/TestDatabaseAutoConfiguration.java index 2f2e13df14..72f7dbc8eb 100644 --- a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/orm/jpa/TestDatabaseAutoConfiguration.java +++ b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/jdbc/TestDatabaseAutoConfiguration.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.boot.test.autoconfigure.orm.jpa; +package org.springframework.boot.test.autoconfigure.jdbc; import javax.sql.DataSource; diff --git a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/orm/jpa/AutoConfigureTestDatabase.java b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/orm/jpa/AutoConfigureTestDatabase.java index 79661049dd..5234da155c 100644 --- a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/orm/jpa/AutoConfigureTestDatabase.java +++ b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/orm/jpa/AutoConfigureTestDatabase.java @@ -33,8 +33,8 @@ import org.springframework.boot.test.autoconfigure.properties.PropertyMapping; * Annotation that can be applied to a test class to configure a test database to use * instead of any application defined or auto-configured {@link DataSource}. * - * @author Phillip Webb - * @see TestDatabaseAutoConfiguration + * @author Stephane Nicoll + * @deprecated since 1.5 in favour of {@link org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase} */ @Target({ ElementType.TYPE, ElementType.METHOD }) @Retention(RetentionPolicy.RUNTIME) @@ -42,6 +42,7 @@ import org.springframework.boot.test.autoconfigure.properties.PropertyMapping; @Inherited @ImportAutoConfiguration @PropertyMapping("spring.test.database") +@Deprecated public @interface AutoConfigureTestDatabase { /** @@ -60,6 +61,7 @@ public @interface AutoConfigureTestDatabase { /** * What the test database can replace. */ + @Deprecated enum Replace { /** diff --git a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/orm/jpa/DataJpaTest.java b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/orm/jpa/DataJpaTest.java index 8d29513ead..af59751113 100644 --- a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/orm/jpa/DataJpaTest.java +++ b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/orm/jpa/DataJpaTest.java @@ -28,6 +28,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.test.autoconfigure.OverrideAutoConfiguration; import org.springframework.boot.test.autoconfigure.core.AutoConfigureCache; import org.springframework.boot.test.autoconfigure.filter.TypeExcludeFilters; +import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; import org.springframework.boot.test.autoconfigure.properties.PropertyMapping; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTestContextBootstrapper; diff --git a/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring.factories b/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring.factories index 722162a648..ed62fd45e4 100644 --- a/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring.factories +++ b/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring.factories @@ -13,6 +13,20 @@ org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration,\ org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\ org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration +# AutoConfigureJdbc auto-configuration imports +org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureJdbc=\ +org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration,\ +org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\ +org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,\ +org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration,\ +org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration,\ +org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration + +# AutoConfigureTestDatabase auto-configuration imports +org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase=\ +org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration,\ +org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration + # AutoConfigureJson auto-configuration imports org.springframework.boot.test.autoconfigure.json.AutoConfigureJson=\ org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration,\ @@ -39,7 +53,7 @@ org.springframework.boot.test.autoconfigure.restdocs.RestDocsAutoConfiguration # AutoConfigureTestDatabase auto-configuration imports org.springframework.boot.test.autoconfigure.orm.jpa.AutoConfigureTestDatabase=\ -org.springframework.boot.test.autoconfigure.orm.jpa.TestDatabaseAutoConfiguration,\ +org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration,\ org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration # AutoConfigureTestEntityManager auto-configuration imports diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/AutoConfigureTestDatabaseWithMultipleDatasourcesIntegrationTests.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/AutoConfigureTestDatabaseWithMultipleDatasourcesIntegrationTests.java similarity index 84% rename from spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/AutoConfigureTestDatabaseWithMultipleDatasourcesIntegrationTests.java rename to spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/AutoConfigureTestDatabaseWithMultipleDatasourcesIntegrationTests.java index 5428b5da9c..e82d73c3f7 100644 --- a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/AutoConfigureTestDatabaseWithMultipleDatasourcesIntegrationTests.java +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/AutoConfigureTestDatabaseWithMultipleDatasourcesIntegrationTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.boot.test.autoconfigure.orm.jpa; +package org.springframework.boot.test.autoconfigure.jdbc; import javax.sql.DataSource; @@ -39,27 +39,13 @@ import static org.assertj.core.api.Assertions.assertThat; * @author Greg Potter */ @RunWith(SpringRunner.class) -@DataJpaTest +@JdbcTest @AutoConfigureTestDatabase public class AutoConfigureTestDatabaseWithMultipleDatasourcesIntegrationTests { - @Autowired - private TestEntityManager entities; - - @Autowired - private ExampleRepository repository; - @Autowired private DataSource dataSource; - @Test - public void testRepository() throws Exception { - this.entities.persist(new ExampleEntity("boot", "124")); - this.entities.flush(); - ExampleEntity found = this.repository.findByReference("124"); - assertThat(found.getName()).isEqualTo("boot"); - } - @Test public void replacesDefinedDataSourceWithExplicit() throws Exception { // Look that the datasource is replaced with an H2 DB. diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/AutoConfigureTestDatabaseWithNoDatabaseIntegrationTests.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/AutoConfigureTestDatabaseWithNoDatabaseIntegrationTests.java similarity index 96% rename from spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/AutoConfigureTestDatabaseWithNoDatabaseIntegrationTests.java rename to spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/AutoConfigureTestDatabaseWithNoDatabaseIntegrationTests.java index d6967972d6..3b8977d54a 100644 --- a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/AutoConfigureTestDatabaseWithNoDatabaseIntegrationTests.java +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/AutoConfigureTestDatabaseWithNoDatabaseIntegrationTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.boot.test.autoconfigure.orm.jpa; +package org.springframework.boot.test.autoconfigure.jdbc; import javax.sql.DataSource; diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/ExampleEntity.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/ExampleEntity.java new file mode 100644 index 0000000000..c674258b4a --- /dev/null +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/ExampleEntity.java @@ -0,0 +1,51 @@ +/* + * Copyright 2012-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.autoconfigure.jdbc; + +/** + * Example entity used with {@link JdbcTest} tests. + * + * @author Stephane Nicoll + */ +public class ExampleEntity { + + private final int id; + + private String name; + + public ExampleEntity(int id, String name) { + this.id = id; + this.name = name; + } + + public ExampleEntity(int id) { + this(id, null); + } + + public int getId() { + return this.id; + } + + public String getName() { + return this.name; + } + + public void setName(String name) { + this.name = name; + } + +} diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/ExampleJdbcApplication.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/ExampleJdbcApplication.java new file mode 100644 index 0000000000..38b3a03a1d --- /dev/null +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/ExampleJdbcApplication.java @@ -0,0 +1,41 @@ +/* + * Copyright 2012-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.autoconfigure.jdbc; + +import javax.sql.DataSource; + +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; + +/** + * Example {@link SpringBootApplication} used with {@link JdbcTest} tests. + * + * @author Phillip Webb + */ +@SpringBootApplication +public class ExampleJdbcApplication { + + @Bean + public DataSource dataSource() { + EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder() + .setType(EmbeddedDatabaseType.HSQL); + return builder.build(); + } + +} diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/ExampleRepository.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/ExampleRepository.java new file mode 100644 index 0000000000..6247b2e418 --- /dev/null +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/ExampleRepository.java @@ -0,0 +1,70 @@ +/* + * Copyright 2012-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.autoconfigure.jdbc; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Collection; + +import javax.transaction.Transactional; + +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.RowMapper; +import org.springframework.stereotype.Repository; + +/** + * Example repository used with {@link JdbcTest} tests. + * + * @author Stephane Nicoll + */ +@Repository +public class ExampleRepository { + + private static final ExampleEntityRowMapper ROW_MAPPER = new ExampleEntityRowMapper(); + + private final JdbcTemplate jdbcTemplate; + + public ExampleRepository(JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } + + @Transactional + public void save(ExampleEntity entity) { + this.jdbcTemplate.update("insert into example (id, name) values (?, ?)", + entity.getId(), entity.getName()); + } + + public ExampleEntity findById(int id) { + return this.jdbcTemplate.queryForObject("select id, name from example where id =?", + new Object[] { id }, ROW_MAPPER); + } + + public Collection findAll() { + return this.jdbcTemplate.query("select id, name from example", ROW_MAPPER); + } + + private static class ExampleEntityRowMapper implements RowMapper { + + @Override + public ExampleEntity mapRow(ResultSet rs, int rowNum) throws SQLException { + int id = rs.getInt("id"); + String name = rs.getString("name"); + return new ExampleEntity(id, name); + } + } + +} diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/JdbcTestIntegrationTests.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/JdbcTestIntegrationTests.java new file mode 100644 index 0000000000..8415274e14 --- /dev/null +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/JdbcTestIntegrationTests.java @@ -0,0 +1,98 @@ +/* + * Copyright 2012-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.autoconfigure.jdbc; + +import java.util.Collection; + +import javax.sql.DataSource; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.NoSuchBeanDefinitionException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration; +import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration; +import org.springframework.context.ApplicationContext; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.boot.test.autoconfigure.AutoConfigurationImportedCondition.importedAutoConfiguration; + +/** + * Integration tests for {@link JdbcTest}. + * + * @author Stephane Nicoll + */ +@RunWith(SpringRunner.class) +@JdbcTest +@TestPropertySource(properties = "spring.datasource.schema=classpath:org/springframework/boot/test/autoconfigure/jdbc/schema.sql") +public class JdbcTestIntegrationTests { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Autowired + private JdbcTemplate jdbcTemplate; + + @Autowired + private DataSource dataSource; + + @Autowired + private ApplicationContext applicationContext; + + @Test + public void testJdbcTemplate() { + ExampleRepository repository = new ExampleRepository(this.jdbcTemplate); + repository.save(new ExampleEntity(1, "John")); + Collection entities = repository.findAll(); + assertThat(entities).hasSize(1); + ExampleEntity entity = entities.iterator().next(); + assertThat(entity.getId()).isEqualTo(1); + assertThat(entity.getName()).isEqualTo("John"); + } + + @Test + public void replacesDefinedDataSourceWithEmbeddedDefault() throws Exception { + String product = this.dataSource.getConnection().getMetaData() + .getDatabaseProductName(); + assertThat(product).isEqualTo("H2"); + } + + @Test + public void didNotInjectExampleRepository() { + this.thrown.expect(NoSuchBeanDefinitionException.class); + this.applicationContext.getBean(ExampleRepository.class); + } + + @Test + public void flywayAutoConfigurationWasImported() { + assertThat(this.applicationContext) + .has(importedAutoConfiguration(FlywayAutoConfiguration.class)); + } + + @Test + public void liquibaseAutoConfigurationWasImported() { + assertThat(this.applicationContext) + .has(importedAutoConfiguration(LiquibaseAutoConfiguration.class)); + } + +} diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/JdbcTestWithAutoConfigureTestDatabaseReplaceAutoConfiguredIntegrationTests.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/JdbcTestWithAutoConfigureTestDatabaseReplaceAutoConfiguredIntegrationTests.java new file mode 100644 index 0000000000..1fd4d7789f --- /dev/null +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/JdbcTestWithAutoConfigureTestDatabaseReplaceAutoConfiguredIntegrationTests.java @@ -0,0 +1,59 @@ +/* + * Copyright 2012-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.autoconfigure.jdbc; + +import javax.sql.DataSource; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.jdbc.EmbeddedDatabaseConnection; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Integration tests for {@link JdbcTest}. + * + * @author Phillip Webb + * @author Stephane Nicoll + */ +@RunWith(SpringRunner.class) +@JdbcTest +@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.AUTO_CONFIGURED, connection = EmbeddedDatabaseConnection.HSQL) +public class JdbcTestWithAutoConfigureTestDatabaseReplaceAutoConfiguredIntegrationTests { + + @Autowired + private DataSource dataSource; + + @Test + public void replacesAutoConfiguredDataSource() throws Exception { + String product = this.dataSource.getConnection().getMetaData() + .getDatabaseProductName(); + assertThat(product).startsWith("HSQL"); + } + + @Configuration + @EnableAutoConfiguration // Will auto-configure H2 + static class Config { + + } + +} diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/JdbcTestWithAutoConfigureTestDatabaseReplaceAutoConfiguredWithoutOverrideIntegrationTests.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/JdbcTestWithAutoConfigureTestDatabaseReplaceAutoConfiguredWithoutOverrideIntegrationTests.java new file mode 100644 index 0000000000..09d8f1770a --- /dev/null +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/JdbcTestWithAutoConfigureTestDatabaseReplaceAutoConfiguredWithoutOverrideIntegrationTests.java @@ -0,0 +1,51 @@ +/* + * Copyright 2012-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.autoconfigure.jdbc; + +import javax.sql.DataSource; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Integration tests for {@link JdbcTest}. + * + * @author Phillip Webb + * @author Stephane Nicoll + */ +@RunWith(SpringRunner.class) +@JdbcTest +@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.AUTO_CONFIGURED) +public class JdbcTestWithAutoConfigureTestDatabaseReplaceAutoConfiguredWithoutOverrideIntegrationTests { + + @Autowired + private DataSource dataSource; + + @Test + public void usesDefaultEmbeddedDatabase() throws Exception { + String product = this.dataSource.getConnection().getMetaData() + .getDatabaseProductName(); + // @AutoConfigureTestDatabase would use H2 but HSQL is manually defined + assertThat(product).startsWith("HSQL"); + } + +} diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/JdbcTestWithAutoConfigureTestDatabaseReplaceExplicitIntegrationTests.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/JdbcTestWithAutoConfigureTestDatabaseReplaceExplicitIntegrationTests.java new file mode 100644 index 0000000000..fb11d995cf --- /dev/null +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/JdbcTestWithAutoConfigureTestDatabaseReplaceExplicitIntegrationTests.java @@ -0,0 +1,70 @@ +/* + * Copyright 2012-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.autoconfigure.jdbc; + +import javax.sql.DataSource; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.jdbc.EmbeddedDatabaseConnection; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Integration tests for {@link JdbcTest}. + * + * @author Phillip Webb + * @author Stephane Nicoll + */ +@RunWith(SpringRunner.class) +@JdbcTest +@AutoConfigureTestDatabase(connection = EmbeddedDatabaseConnection.HSQL) +public class JdbcTestWithAutoConfigureTestDatabaseReplaceExplicitIntegrationTests { + + @Autowired + private DataSource dataSource; + + @Test + public void replacesDefinedDataSourceWithExplicit() throws Exception { + // H2 is explicitly defined but HSQL is the override. + String product = this.dataSource.getConnection().getMetaData() + .getDatabaseProductName(); + assertThat(product).startsWith("HSQL"); + } + + @Configuration + @EnableAutoConfiguration + static class Config { + + @Bean + public DataSource dataSource() { + EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder() + .setType(EmbeddedDatabaseType.H2); + return builder.build(); + } + + } + +} diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/JdbcTestWithAutoConfigureTestDatabaseReplaceNoneIntegrationTests.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/JdbcTestWithAutoConfigureTestDatabaseReplaceNoneIntegrationTests.java new file mode 100644 index 0000000000..e016b37bcc --- /dev/null +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/JdbcTestWithAutoConfigureTestDatabaseReplaceNoneIntegrationTests.java @@ -0,0 +1,51 @@ +/* + * Copyright 2012-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.autoconfigure.jdbc; + +import javax.sql.DataSource; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Integration tests for {@link JdbcTest}. + * + * @author Phillip Webb + * @author Stephane Nicoll + */ +@RunWith(SpringRunner.class) +@JdbcTest +@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) +public class JdbcTestWithAutoConfigureTestDatabaseReplaceNoneIntegrationTests { + + @Autowired + private DataSource dataSource; + + @Test + public void usesDefaultEmbeddedDatabase() throws Exception { + // HSQL is explicitly defined and should not be replaced + String product = this.dataSource.getConnection().getMetaData() + .getDatabaseProductName(); + assertThat(product).startsWith("HSQL"); + } + +} diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/JdbcTestWithIncludeFilterIntegrationTests.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/JdbcTestWithIncludeFilterIntegrationTests.java new file mode 100644 index 0000000000..761fb0030f --- /dev/null +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/JdbcTestWithIncludeFilterIntegrationTests.java @@ -0,0 +1,51 @@ +/* + * Copyright 2012-2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.autoconfigure.jdbc; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.ComponentScan.Filter; +import org.springframework.stereotype.Repository; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Integration test with custom include filter for {@link JdbcTest}. + * + * @author Stephane Nicoll + */ +@RunWith(SpringRunner.class) +@JdbcTest(includeFilters = @Filter(Repository.class)) +@TestPropertySource(properties = "spring.datasource.schema=classpath:org/springframework/boot/test/autoconfigure/jdbc/schema.sql") +public class JdbcTestWithIncludeFilterIntegrationTests { + + @Autowired + private ExampleRepository repository; + + @Test + public void testRepository() { + this.repository.save(new ExampleEntity(42, "Smith")); + ExampleEntity entity = this.repository.findById(42); + assertThat(entity).isNotNull(); + assertThat(entity.getName()).isEqualTo("Smith"); + } + +} diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/TestDatabaseAutoConfigurationTests.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/TestDatabaseAutoConfigurationTests.java similarity index 98% rename from spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/TestDatabaseAutoConfigurationTests.java rename to spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/TestDatabaseAutoConfigurationTests.java index 8f20b2d232..301f50232f 100644 --- a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/TestDatabaseAutoConfigurationTests.java +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/TestDatabaseAutoConfigurationTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.boot.test.autoconfigure.orm.jpa; +package org.springframework.boot.test.autoconfigure.jdbc; import javax.sql.DataSource; diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/DataJpaTestWithAutoConfigureTestDatabaseReplaceAutoConfiguredIntegrationTests.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/DataJpaTestWithAutoConfigureTestDatabaseReplaceAutoConfiguredIntegrationTests.java index 028a844325..afaba47b76 100644 --- a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/DataJpaTestWithAutoConfigureTestDatabaseReplaceAutoConfiguredIntegrationTests.java +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/DataJpaTestWithAutoConfigureTestDatabaseReplaceAutoConfiguredIntegrationTests.java @@ -38,6 +38,7 @@ import static org.assertj.core.api.Assertions.assertThat; @RunWith(SpringRunner.class) @DataJpaTest @AutoConfigureTestDatabase(replace = Replace.AUTO_CONFIGURED, connection = EmbeddedDatabaseConnection.HSQL) +@Deprecated public class DataJpaTestWithAutoConfigureTestDatabaseReplaceAutoConfiguredIntegrationTests { @Autowired diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/DataJpaTestWithAutoConfigureTestDatabaseReplaceAutoConfiguredWithoutOverrideIntegrationTests.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/DataJpaTestWithAutoConfigureTestDatabaseReplaceAutoConfiguredWithoutOverrideIntegrationTests.java index 029810eb4f..52b571609e 100644 --- a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/DataJpaTestWithAutoConfigureTestDatabaseReplaceAutoConfiguredWithoutOverrideIntegrationTests.java +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/DataJpaTestWithAutoConfigureTestDatabaseReplaceAutoConfiguredWithoutOverrideIntegrationTests.java @@ -35,6 +35,7 @@ import static org.assertj.core.api.Assertions.assertThat; @RunWith(SpringRunner.class) @DataJpaTest @AutoConfigureTestDatabase(replace = Replace.AUTO_CONFIGURED) +@Deprecated public class DataJpaTestWithAutoConfigureTestDatabaseReplaceAutoConfiguredWithoutOverrideIntegrationTests { @Autowired diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/DataJpaTestWithAutoConfigureTestDatabaseReplaceExplicitIntegrationTests.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/DataJpaTestWithAutoConfigureTestDatabaseReplaceExplicitIntegrationTests.java index e8350fe6b5..0a373f23a5 100644 --- a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/DataJpaTestWithAutoConfigureTestDatabaseReplaceExplicitIntegrationTests.java +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/DataJpaTestWithAutoConfigureTestDatabaseReplaceExplicitIntegrationTests.java @@ -40,6 +40,7 @@ import static org.assertj.core.api.Assertions.assertThat; @RunWith(SpringRunner.class) @DataJpaTest @AutoConfigureTestDatabase(connection = EmbeddedDatabaseConnection.HSQL) +@Deprecated public class DataJpaTestWithAutoConfigureTestDatabaseReplaceExplicitIntegrationTests { @Autowired diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/DataJpaTestWithAutoConfigureTestDatabaseReplaceNoneIntegrationTests.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/DataJpaTestWithAutoConfigureTestDatabaseReplaceNoneIntegrationTests.java index bef5f4b949..792ff399db 100644 --- a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/DataJpaTestWithAutoConfigureTestDatabaseReplaceNoneIntegrationTests.java +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/DataJpaTestWithAutoConfigureTestDatabaseReplaceNoneIntegrationTests.java @@ -35,6 +35,7 @@ import static org.assertj.core.api.Assertions.assertThat; @RunWith(SpringRunner.class) @DataJpaTest @AutoConfigureTestDatabase(replace = Replace.NONE) +@Deprecated public class DataJpaTestWithAutoConfigureTestDatabaseReplaceNoneIntegrationTests { @Autowired diff --git a/spring-boot-test-autoconfigure/src/test/resources/org/springframework/boot/test/autoconfigure/jdbc/schema.sql b/spring-boot-test-autoconfigure/src/test/resources/org/springframework/boot/test/autoconfigure/jdbc/schema.sql new file mode 100644 index 0000000000..c7559fe8cc --- /dev/null +++ b/spring-boot-test-autoconfigure/src/test/resources/org/springframework/boot/test/autoconfigure/jdbc/schema.sql @@ -0,0 +1 @@ +create table example (id int, name varchar); \ No newline at end of file