Merge pull request #29003 from raviu
* pr/29003: Polish "Include AbstractJdbcConfiguration beans in @DataJdbcTest" Include AbstractJdbcConfiguration beans in @DataJdbcTest Closes gh-29003
This commit is contained in:
commit
af933f24c1
|
@ -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-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<DataJdbcTest> {
|
||||
|
||||
private static final Set<Class<?>> DEFAULT_INCLUDES = Collections.singleton(AbstractJdbcConfiguration.class);
|
||||
|
||||
DataJdbcTypeExcludeFilter(Class<?> testClass) {
|
||||
super(testClass);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Set<Class<?>> getDefaultIncludes() {
|
||||
return DEFAULT_INCLUDES;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue