Polish "Include AbstractJdbcConfiguration beans in @DataJdbcTest"
See gh-29003
This commit is contained in:
@@ -595,7 +595,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 <<test-auto-configuration#test-auto-configuration,found in the appendix>>.
|
||||
|
||||
@@ -43,8 +43,9 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
* Annotation that can be used for a Data JDBC test that focuses <strong>only</strong> on
|
||||
* Data JDBC components.
|
||||
* <p>
|
||||
* 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.
|
||||
* <p>
|
||||
* 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
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2021 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,14 +16,13 @@
|
||||
|
||||
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;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* {@link TypeExcludeFilter} for {@link DataJdbcTest @DataJdbcTest}.
|
||||
*
|
||||
@@ -33,12 +32,7 @@ import java.util.Set;
|
||||
*/
|
||||
public final class DataJdbcTypeExcludeFilter extends StandardAnnotationCustomizableTypeExcludeFilter<DataJdbcTest> {
|
||||
|
||||
private static final Set<Class<?>> DEFAULT_INCLUDES;
|
||||
static {
|
||||
Set<Class<?>> includes = new LinkedHashSet<>();
|
||||
includes.add(AbstractJdbcConfiguration.class);
|
||||
DEFAULT_INCLUDES = Collections.unmodifiableSet(includes);
|
||||
}
|
||||
private static final Set<Class<?>> DEFAULT_INCLUDES = Collections.singleton(AbstractJdbcConfiguration.class);
|
||||
|
||||
DataJdbcTypeExcludeFilter(Class<?> testClass) {
|
||||
super(testClass);
|
||||
@@ -48,4 +42,5 @@ public final class DataJdbcTypeExcludeFilter extends StandardAnnotationCustomiza
|
||||
protected Set<Class<?>> getDefaultIncludes() {
|
||||
return DEFAULT_INCLUDES;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,35 +16,37 @@
|
||||
|
||||
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 java.io.IOException;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link DataJdbcTypeExcludeFilter}.
|
||||
*
|
||||
* @author Ravi Undupitiya
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class DataJdbcTypeExcludeFilterTests {
|
||||
class DataJdbcTypeExcludeFilterTests {
|
||||
|
||||
private MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
|
||||
|
||||
@Test
|
||||
void matchNotUsingDefaultFilters() throws Exception {
|
||||
DataJdbcTypeExcludeFilter filter = new DataJdbcTypeExcludeFilter(NotUsingDefaultFilters.class);
|
||||
assertThat(excludes(filter, AbstractJdbcConfiguration.class)).isTrue();
|
||||
}
|
||||
private final MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory();
|
||||
|
||||
@Test
|
||||
void matchUsingDefaultFilters() throws Exception {
|
||||
DataJdbcTypeExcludeFilter filter = new DataJdbcTypeExcludeFilter(UsingDefaultFilters.class);
|
||||
assertThat(excludes(filter, AbstractJdbcConfiguration.class)).isFalse();
|
||||
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 {
|
||||
@@ -62,4 +64,8 @@ public class DataJdbcTypeExcludeFilterTests {
|
||||
|
||||
}
|
||||
|
||||
static class TestJdbcConfiguration extends AbstractJdbcConfiguration {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user