Skip to content

Commit 9b34c31

Browse files
committed
Polish "Include AbstractJdbcConfiguration beans in @DataJdbcTest"
See gh-29003
1 parent 7f8dd81 commit 9b34c31

File tree

4 files changed

+28
-26
lines changed

4 files changed

+28
-26
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: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 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,14 +16,13 @@
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;
2124
import org.springframework.data.jdbc.repository.config.AbstractJdbcConfiguration;
2225

23-
import java.util.Collections;
24-
import java.util.LinkedHashSet;
25-
import java.util.Set;
26-
2726
/**
2827
* {@link TypeExcludeFilter} for {@link DataJdbcTest @DataJdbcTest}.
2928
*
@@ -33,12 +32,7 @@
3332
*/
3433
public final class DataJdbcTypeExcludeFilter extends StandardAnnotationCustomizableTypeExcludeFilter<DataJdbcTest> {
3534

36-
private static final Set<Class<?>> DEFAULT_INCLUDES;
37-
static {
38-
Set<Class<?>> includes = new LinkedHashSet<>();
39-
includes.add(AbstractJdbcConfiguration.class);
40-
DEFAULT_INCLUDES = Collections.unmodifiableSet(includes);
41-
}
35+
private static final Set<Class<?>> DEFAULT_INCLUDES = Collections.singleton(AbstractJdbcConfiguration.class);
4236

4337
DataJdbcTypeExcludeFilter(Class<?> testClass) {
4438
super(testClass);
@@ -48,4 +42,5 @@ public final class DataJdbcTypeExcludeFilter extends StandardAnnotationCustomiza
4842
protected Set<Class<?>> getDefaultIncludes() {
4943
return DEFAULT_INCLUDES;
5044
}
45+
5146
}

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

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,35 +16,37 @@
1616

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

19+
import java.io.IOException;
20+
1921
import org.junit.jupiter.api.Test;
22+
2023
import org.springframework.core.type.classreading.MetadataReader;
2124
import org.springframework.core.type.classreading.MetadataReaderFactory;
2225
import org.springframework.core.type.classreading.SimpleMetadataReaderFactory;
2326
import org.springframework.data.jdbc.repository.config.AbstractJdbcConfiguration;
2427

25-
import java.io.IOException;
26-
2728
import static org.assertj.core.api.Assertions.assertThat;
2829

2930
/**
3031
* Tests for {@link DataJdbcTypeExcludeFilter}.
3132
*
3233
* @author Ravi Undupitiya
34+
* @author Stephane Nicoll
3335
*/
34-
public class DataJdbcTypeExcludeFilterTests {
36+
class DataJdbcTypeExcludeFilterTests {
3537

36-
private MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
38+
private final MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
3739

3840
@Test
39-
void matchNotUsingDefaultFilters() throws Exception {
40-
DataJdbcTypeExcludeFilter filter = new DataJdbcTypeExcludeFilter(NotUsingDefaultFilters.class);
41-
assertThat(excludes(filter, AbstractJdbcConfiguration.class)).isTrue();
41+
void matchUsingDefaultFilters() throws Exception {
42+
DataJdbcTypeExcludeFilter filter = new DataJdbcTypeExcludeFilter(UsingDefaultFilters.class);
43+
assertThat(excludes(filter, TestJdbcConfiguration.class)).isFalse();
4244
}
4345

4446
@Test
45-
void matchUsingDefaultFilters() throws Exception {
46-
DataJdbcTypeExcludeFilter filter = new DataJdbcTypeExcludeFilter(UsingDefaultFilters.class);
47-
assertThat(excludes(filter, AbstractJdbcConfiguration.class)).isFalse();
47+
void matchNotUsingDefaultFilters() throws Exception {
48+
DataJdbcTypeExcludeFilter filter = new DataJdbcTypeExcludeFilter(NotUsingDefaultFilters.class);
49+
assertThat(excludes(filter, TestJdbcConfiguration.class)).isTrue();
4850
}
4951

5052
private boolean excludes(DataJdbcTypeExcludeFilter filter, Class<?> type) throws IOException {
@@ -62,4 +64,8 @@ static class NotUsingDefaultFilters {
6264

6365
}
6466

67+
static class TestJdbcConfiguration extends AbstractJdbcConfiguration {
68+
69+
}
70+
6571
}

0 commit comments

Comments
 (0)