Skip to content

Commit af933f2

Browse files
committed
Merge pull request #29003 from raviu
* pr/29003: Polish "Include AbstractJdbcConfiguration beans in @DataJdbcTest" Include AbstractJdbcConfiguration beans in @DataJdbcTest Closes gh-29003
2 parents 05fa0e2 + 9b34c31 commit af933f2

File tree

4 files changed

+90
-6
lines changed

4 files changed

+90
-6
lines changed

spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/testing.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ If you prefer your test to run against a real database, you can use the `@AutoCo
595595
==== Auto-configured Data JDBC Tests
596596
`@DataJdbcTest` is similar to `@JdbcTest` but is for tests that use Spring Data JDBC repositories.
597597
By default, it configures an in-memory embedded database, a `JdbcTemplate`, and Spring Data JDBC repositories.
598-
Regular `@Component` and `@ConfigurationProperties` beans are not scanned when the `@DataJdbcTest` annotation is used.
598+
Only `AbstractJdbcConfiguration` sub-classes are scanned when the `@DataJdbcTest` annotation is used, regular `@Component` and `@ConfigurationProperties` beans are not scanned.
599599
`@EnableConfigurationProperties` can be used to include `@ConfigurationProperties` beans.
600600

601601
TIP: A list of the auto-configurations that are enabled by `@DataJdbcTest` can be <<test-auto-configuration#test-auto-configuration,found in the appendix>>.

spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/jdbc/DataJdbcTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@
4343
* Annotation that can be used for a Data JDBC test that focuses <strong>only</strong> on
4444
* Data JDBC components.
4545
* <p>
46-
* Using this annotation will disable full auto-configuration and instead apply only
47-
* configuration relevant to Data JDBC tests.
46+
* Using this annotation will disable full auto-configuration, scan for
47+
* {@code AbstractJdbcConfiguration} sub-classes, and apply only configuration relevant to
48+
* Data JDBC tests.
4849
* <p>
4950
* By default, tests annotated with {@code @DataJdbcTest} are transactional and roll back
5051
* at the end of each test. They also use an embedded in-memory database (replacing any
@@ -87,8 +88,8 @@
8788

8889
/**
8990
* Determines if default filtering should be used with
90-
* {@link SpringBootApplication @SpringBootApplication}. By default no beans are
91-
* included.
91+
* {@link SpringBootApplication @SpringBootApplication}. By default, only
92+
* {@code AbstractJdbcConfiguration} beans are included.
9293
* @see #includeFilters()
9394
* @see #excludeFilters()
9495
* @return if default filters should be used

spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/jdbc/DataJdbcTypeExcludeFilter.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,19 +16,31 @@
1616

1717
package org.springframework.boot.test.autoconfigure.data.jdbc;
1818

19+
import java.util.Collections;
20+
import java.util.Set;
21+
1922
import org.springframework.boot.context.TypeExcludeFilter;
2023
import org.springframework.boot.test.autoconfigure.filter.StandardAnnotationCustomizableTypeExcludeFilter;
24+
import org.springframework.data.jdbc.repository.config.AbstractJdbcConfiguration;
2125

2226
/**
2327
* {@link TypeExcludeFilter} for {@link DataJdbcTest @DataJdbcTest}.
2428
*
2529
* @author Andy Wilkinson
30+
* @author Ravi Undupitiya
2631
* @since 2.2.1
2732
*/
2833
public final class DataJdbcTypeExcludeFilter extends StandardAnnotationCustomizableTypeExcludeFilter<DataJdbcTest> {
2934

35+
private static final Set<Class<?>> DEFAULT_INCLUDES = Collections.singleton(AbstractJdbcConfiguration.class);
36+
3037
DataJdbcTypeExcludeFilter(Class<?> testClass) {
3138
super(testClass);
3239
}
3340

41+
@Override
42+
protected Set<Class<?>> getDefaultIncludes() {
43+
return DEFAULT_INCLUDES;
44+
}
45+
3446
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright 2012-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test.autoconfigure.data.jdbc;
18+
19+
import java.io.IOException;
20+
21+
import org.junit.jupiter.api.Test;
22+
23+
import org.springframework.core.type.classreading.MetadataReader;
24+
import org.springframework.core.type.classreading.MetadataReaderFactory;
25+
import org.springframework.core.type.classreading.SimpleMetadataReaderFactory;
26+
import org.springframework.data.jdbc.repository.config.AbstractJdbcConfiguration;
27+
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
30+
/**
31+
* Tests for {@link DataJdbcTypeExcludeFilter}.
32+
*
33+
* @author Ravi Undupitiya
34+
* @author Stephane Nicoll
35+
*/
36+
class DataJdbcTypeExcludeFilterTests {
37+
38+
private final MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
39+
40+
@Test
41+
void matchUsingDefaultFilters() throws Exception {
42+
DataJdbcTypeExcludeFilter filter = new DataJdbcTypeExcludeFilter(UsingDefaultFilters.class);
43+
assertThat(excludes(filter, TestJdbcConfiguration.class)).isFalse();
44+
}
45+
46+
@Test
47+
void matchNotUsingDefaultFilters() throws Exception {
48+
DataJdbcTypeExcludeFilter filter = new DataJdbcTypeExcludeFilter(NotUsingDefaultFilters.class);
49+
assertThat(excludes(filter, TestJdbcConfiguration.class)).isTrue();
50+
}
51+
52+
private boolean excludes(DataJdbcTypeExcludeFilter filter, Class<?> type) throws IOException {
53+
MetadataReader metadataReader = this.metadataReaderFactory.getMetadataReader(type.getName());
54+
return filter.match(metadataReader, this.metadataReaderFactory);
55+
}
56+
57+
@DataJdbcTest
58+
static class UsingDefaultFilters {
59+
60+
}
61+
62+
@DataJdbcTest(useDefaultFilters = false)
63+
static class NotUsingDefaultFilters {
64+
65+
}
66+
67+
static class TestJdbcConfiguration extends AbstractJdbcConfiguration {
68+
69+
}
70+
71+
}

0 commit comments

Comments
 (0)