diff --git a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/testing.adoc b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/testing.adoc index f7250beafea..97bd79710b3 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/testing.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/testing.adoc @@ -539,7 +539,7 @@ If you prefer your test to run against a real database, you can use the `@AutoCo ==== Auto-configured Data JDBC Tests `@DataJdbcTest` is similar to `@JdbcTest` but is for tests that use Spring Data JDBC repositories. By default, it configures an in-memory embedded database, a `JdbcTemplate`, and Spring Data JDBC repositories. -Regular `@Component` and `@ConfigurationProperties` beans are not scanned when the `@DataJdbcTest` annotation is used. +Only `AbstractJdbcConfiguration` sub-classes are scanned when the `@DataJdbcTest` annotation is used, regular `@Component` and `@ConfigurationProperties` beans are not scanned. `@EnableConfigurationProperties` can be used to include `@ConfigurationProperties` beans. TIP: A list of the auto-configurations that are enabled by `@DataJdbcTest` can be <>. diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/jdbc/DataJdbcTest.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/jdbc/DataJdbcTest.java index be7d58b20be..bdcbc890a77 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/jdbc/DataJdbcTest.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/jdbc/DataJdbcTest.java @@ -43,8 +43,9 @@ import org.springframework.transaction.annotation.Transactional; * Annotation that can be used for a Data JDBC test that focuses only on * Data JDBC components. *

- * Using this annotation will disable full auto-configuration and instead apply only - * configuration relevant to Data JDBC tests. + * Using this annotation will disable full auto-configuration, scan for + * {@code AbstractJdbcConfiguration} sub-classes, and apply only configuration relevant to + * Data JDBC tests. *

* By default, tests annotated with {@code @DataJdbcTest} are transactional and roll back * at the end of each test. They also use an embedded in-memory database (replacing any @@ -87,8 +88,8 @@ public @interface DataJdbcTest { /** * Determines if default filtering should be used with - * {@link SpringBootApplication @SpringBootApplication}. By default no beans are - * included. + * {@link SpringBootApplication @SpringBootApplication}. By default, only + * {@code AbstractJdbcConfiguration} beans are included. * @see #includeFilters() * @see #excludeFilters() * @return if default filters should be used diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/jdbc/DataJdbcTypeExcludeFilter.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/jdbc/DataJdbcTypeExcludeFilter.java index 989c8ebc300..acae298e294 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/jdbc/DataJdbcTypeExcludeFilter.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/jdbc/DataJdbcTypeExcludeFilter.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2022 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. @@ -16,19 +16,31 @@ package org.springframework.boot.test.autoconfigure.data.jdbc; +import java.util.Collections; +import java.util.Set; + import org.springframework.boot.context.TypeExcludeFilter; import org.springframework.boot.test.autoconfigure.filter.StandardAnnotationCustomizableTypeExcludeFilter; +import org.springframework.data.jdbc.repository.config.AbstractJdbcConfiguration; /** * {@link TypeExcludeFilter} for {@link DataJdbcTest @DataJdbcTest}. * * @author Andy Wilkinson + * @author Ravi Undupitiya * @since 2.2.1 */ public final class DataJdbcTypeExcludeFilter extends StandardAnnotationCustomizableTypeExcludeFilter { + private static final Set> DEFAULT_INCLUDES = Collections.singleton(AbstractJdbcConfiguration.class); + DataJdbcTypeExcludeFilter(Class testClass) { super(testClass); } + @Override + protected Set> getDefaultIncludes() { + return DEFAULT_INCLUDES; + } + } diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/jdbc/DataJdbcTypeExcludeFilterTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/jdbc/DataJdbcTypeExcludeFilterTests.java new file mode 100644 index 00000000000..1e7a8bda018 --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/jdbc/DataJdbcTypeExcludeFilterTests.java @@ -0,0 +1,71 @@ +/* + * Copyright 2012-2021 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 + * + * https://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.data.jdbc; + +import java.io.IOException; + +import org.junit.jupiter.api.Test; + +import org.springframework.core.type.classreading.MetadataReader; +import org.springframework.core.type.classreading.MetadataReaderFactory; +import org.springframework.core.type.classreading.SimpleMetadataReaderFactory; +import org.springframework.data.jdbc.repository.config.AbstractJdbcConfiguration; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link DataJdbcTypeExcludeFilter}. + * + * @author Ravi Undupitiya + * @author Stephane Nicoll + */ +class DataJdbcTypeExcludeFilterTests { + + private final MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory(); + + @Test + void matchUsingDefaultFilters() throws Exception { + DataJdbcTypeExcludeFilter filter = new DataJdbcTypeExcludeFilter(UsingDefaultFilters.class); + assertThat(excludes(filter, TestJdbcConfiguration.class)).isFalse(); + } + + @Test + void matchNotUsingDefaultFilters() throws Exception { + DataJdbcTypeExcludeFilter filter = new DataJdbcTypeExcludeFilter(NotUsingDefaultFilters.class); + assertThat(excludes(filter, TestJdbcConfiguration.class)).isTrue(); + } + + private boolean excludes(DataJdbcTypeExcludeFilter filter, Class type) throws IOException { + MetadataReader metadataReader = this.metadataReaderFactory.getMetadataReader(type.getName()); + return filter.match(metadataReader, this.metadataReaderFactory); + } + + @DataJdbcTest + static class UsingDefaultFilters { + + } + + @DataJdbcTest(useDefaultFilters = false) + static class NotUsingDefaultFilters { + + } + + static class TestJdbcConfiguration extends AbstractJdbcConfiguration { + + } + +}