Skip to content

Commit 3b4ff7d

Browse files
committed
Disable ConfigurationProperties scanning for slice tests
Closes gh-16659
1 parent 930186e commit 3b4ff7d

File tree

48 files changed

+1454
-3
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1454
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2012-2019 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;
18+
19+
import java.lang.annotation.Documented;
20+
import java.lang.annotation.ElementType;
21+
import java.lang.annotation.Retention;
22+
import java.lang.annotation.RetentionPolicy;
23+
import java.lang.annotation.Target;
24+
25+
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
26+
27+
/**
28+
* Annotation that can be used to override
29+
* {@link ConfigurationPropertiesScan @ConfigurationPropertiesScan}.
30+
*
31+
* @author Madhura Bhave
32+
* @since 2.2.0
33+
* @see ConfigurationPropertiesScan#CONFIGURATION_PROPERTIES_SCAN_ENABLED_PROPERTY
34+
*/
35+
@Target(ElementType.TYPE)
36+
@Retention(RetentionPolicy.RUNTIME)
37+
@Documented
38+
public @interface OverrideConfigurationPropertiesScan {
39+
40+
/**
41+
* The value of the
42+
* {@link ConfigurationPropertiesScan#CONFIGURATION_PROPERTIES_SCAN_ENABLED_PROPERTY
43+
* enabled override property}.
44+
* @return the override value
45+
*/
46+
boolean enabled();
47+
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2012-2019 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;
18+
19+
import java.util.List;
20+
21+
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
22+
import org.springframework.boot.test.util.TestPropertyValues;
23+
import org.springframework.context.ConfigurableApplicationContext;
24+
import org.springframework.core.annotation.MergedAnnotations;
25+
import org.springframework.core.annotation.MergedAnnotations.SearchStrategy;
26+
import org.springframework.test.context.ContextConfigurationAttributes;
27+
import org.springframework.test.context.ContextCustomizer;
28+
import org.springframework.test.context.ContextCustomizerFactory;
29+
import org.springframework.test.context.MergedContextConfiguration;
30+
31+
/**
32+
* {@link ContextCustomizerFactory} to support
33+
* {@link OverrideConfigurationPropertiesScan @OverrideConfigurationPropertiesScan}.
34+
*
35+
* @author Madhura Bhave
36+
*/
37+
class OverrideConfigurationPropertiesScanContextCustomizerFactory
38+
implements ContextCustomizerFactory {
39+
40+
@Override
41+
public ContextCustomizer createContextCustomizer(Class<?> testClass,
42+
List<ContextConfigurationAttributes> configurationAttributes) {
43+
boolean enabled = MergedAnnotations.from(testClass, SearchStrategy.EXHAUSTIVE)
44+
.get(OverrideConfigurationPropertiesScan.class)
45+
.getValue("enabled", Boolean.class).orElse(true);
46+
return !enabled ? new DisableConfigurationPropertiesContextCustomizer() : null;
47+
}
48+
49+
/**
50+
* {@link ContextCustomizer} to disable configuration properties scanning.
51+
*/
52+
private static class DisableConfigurationPropertiesContextCustomizer
53+
implements ContextCustomizer {
54+
55+
@Override
56+
public void customizeContext(ConfigurableApplicationContext context,
57+
MergedContextConfiguration mergedConfig) {
58+
TestPropertyValues.of(
59+
ConfigurationPropertiesScan.CONFIGURATION_PROPERTIES_SCAN_ENABLED_PROPERTY
60+
+ "=false")
61+
.applyTo(context);
62+
}
63+
64+
@Override
65+
public boolean equals(Object obj) {
66+
return (obj != null && obj.getClass() == getClass());
67+
}
68+
69+
@Override
70+
public int hashCode() {
71+
return getClass().hashCode();
72+
}
73+
74+
}
75+
76+
}

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

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
2929
import org.springframework.boot.autoconfigure.SpringBootApplication;
3030
import org.springframework.boot.test.autoconfigure.OverrideAutoConfiguration;
31+
import org.springframework.boot.test.autoconfigure.OverrideConfigurationPropertiesScan;
3132
import org.springframework.boot.test.autoconfigure.core.AutoConfigureCache;
3233
import org.springframework.boot.test.autoconfigure.filter.TypeExcludeFilters;
3334
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
@@ -55,6 +56,7 @@
5556
@BootstrapWith(DataJdbcTestContextBootstrapper.class)
5657
@ExtendWith(SpringExtension.class)
5758
@OverrideAutoConfiguration(enabled = false)
59+
@OverrideConfigurationPropertiesScan(enabled = false)
5860
@TypeExcludeFilters(DataJdbcTypeExcludeFilter.class)
5961
@AutoConfigureCache
6062
@AutoConfigureDataJdbc

spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/ldap/DataLdapTest.java

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
2929
import org.springframework.boot.autoconfigure.SpringBootApplication;
3030
import org.springframework.boot.test.autoconfigure.OverrideAutoConfiguration;
31+
import org.springframework.boot.test.autoconfigure.OverrideConfigurationPropertiesScan;
3132
import org.springframework.boot.test.autoconfigure.core.AutoConfigureCache;
3233
import org.springframework.boot.test.autoconfigure.filter.TypeExcludeFilters;
3334
import org.springframework.context.annotation.ComponentScan.Filter;
@@ -58,6 +59,7 @@
5859
@BootstrapWith(DataLdapTestContextBootstrapper.class)
5960
@ExtendWith(SpringExtension.class)
6061
@OverrideAutoConfiguration(enabled = false)
62+
@OverrideConfigurationPropertiesScan(enabled = false)
6163
@TypeExcludeFilters(DataLdapTypeExcludeFilter.class)
6264
@AutoConfigureCache
6365
@AutoConfigureDataLdap

spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTest.java

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
2929
import org.springframework.boot.autoconfigure.SpringBootApplication;
3030
import org.springframework.boot.test.autoconfigure.OverrideAutoConfiguration;
31+
import org.springframework.boot.test.autoconfigure.OverrideConfigurationPropertiesScan;
3132
import org.springframework.boot.test.autoconfigure.core.AutoConfigureCache;
3233
import org.springframework.boot.test.autoconfigure.filter.TypeExcludeFilters;
3334
import org.springframework.context.annotation.ComponentScan.Filter;
@@ -59,6 +60,7 @@
5960
@BootstrapWith(DataMongoTestContextBootstrapper.class)
6061
@ExtendWith(SpringExtension.class)
6162
@OverrideAutoConfiguration(enabled = false)
63+
@OverrideConfigurationPropertiesScan(enabled = false)
6264
@TypeExcludeFilters(DataMongoTypeExcludeFilter.class)
6365
@AutoConfigureCache
6466
@AutoConfigureDataMongo

spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/neo4j/DataNeo4jTest.java

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
2929
import org.springframework.boot.autoconfigure.SpringBootApplication;
3030
import org.springframework.boot.test.autoconfigure.OverrideAutoConfiguration;
31+
import org.springframework.boot.test.autoconfigure.OverrideConfigurationPropertiesScan;
3132
import org.springframework.boot.test.autoconfigure.core.AutoConfigureCache;
3233
import org.springframework.boot.test.autoconfigure.filter.TypeExcludeFilters;
3334
import org.springframework.context.annotation.ComponentScan.Filter;
@@ -61,6 +62,7 @@
6162
@BootstrapWith(DataNeo4jTestContextBootstrapper.class)
6263
@ExtendWith(SpringExtension.class)
6364
@OverrideAutoConfiguration(enabled = false)
65+
@OverrideConfigurationPropertiesScan(enabled = false)
6466
@TypeExcludeFilters(DataNeo4jTypeExcludeFilter.class)
6567
@Transactional
6668
@AutoConfigureCache

spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/redis/DataRedisTest.java

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
2929
import org.springframework.boot.autoconfigure.SpringBootApplication;
3030
import org.springframework.boot.test.autoconfigure.OverrideAutoConfiguration;
31+
import org.springframework.boot.test.autoconfigure.OverrideConfigurationPropertiesScan;
3132
import org.springframework.boot.test.autoconfigure.core.AutoConfigureCache;
3233
import org.springframework.boot.test.autoconfigure.filter.TypeExcludeFilters;
3334
import org.springframework.context.annotation.ComponentScan.Filter;
@@ -55,6 +56,7 @@
5556
@BootstrapWith(DataRedisTestContextBootstrapper.class)
5657
@ExtendWith(SpringExtension.class)
5758
@OverrideAutoConfiguration(enabled = false)
59+
@OverrideConfigurationPropertiesScan(enabled = false)
5860
@TypeExcludeFilters(DataRedisTypeExcludeFilter.class)
5961
@AutoConfigureCache
6062
@AutoConfigureDataRedis

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

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
2929
import org.springframework.boot.autoconfigure.SpringBootApplication;
3030
import org.springframework.boot.test.autoconfigure.OverrideAutoConfiguration;
31+
import org.springframework.boot.test.autoconfigure.OverrideConfigurationPropertiesScan;
3132
import org.springframework.boot.test.autoconfigure.core.AutoConfigureCache;
3233
import org.springframework.boot.test.autoconfigure.filter.TypeExcludeFilters;
3334
import org.springframework.boot.test.context.SpringBootTest;
@@ -69,6 +70,7 @@
6970
@BootstrapWith(JdbcTestContextBootstrapper.class)
7071
@ExtendWith(SpringExtension.class)
7172
@OverrideAutoConfiguration(enabled = false)
73+
@OverrideConfigurationPropertiesScan(enabled = false)
7274
@TypeExcludeFilters(JdbcTypeExcludeFilter.class)
7375
@Transactional
7476
@AutoConfigureCache

spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/jooq/JooqTest.java

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
2929
import org.springframework.boot.autoconfigure.SpringBootApplication;
3030
import org.springframework.boot.test.autoconfigure.OverrideAutoConfiguration;
31+
import org.springframework.boot.test.autoconfigure.OverrideConfigurationPropertiesScan;
3132
import org.springframework.boot.test.autoconfigure.filter.TypeExcludeFilters;
3233
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
3334
import org.springframework.context.annotation.ComponentScan.Filter;
@@ -62,6 +63,7 @@
6263
@BootstrapWith(JooqTestContextBootstrapper.class)
6364
@ExtendWith(SpringExtension.class)
6465
@OverrideAutoConfiguration(enabled = false)
66+
@OverrideConfigurationPropertiesScan(enabled = false)
6567
@TypeExcludeFilters(JooqTypeExcludeFilter.class)
6668
@Transactional
6769
@AutoConfigureJooq

spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/json/JsonTest.java

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
2929
import org.springframework.boot.autoconfigure.SpringBootApplication;
3030
import org.springframework.boot.test.autoconfigure.OverrideAutoConfiguration;
31+
import org.springframework.boot.test.autoconfigure.OverrideConfigurationPropertiesScan;
3132
import org.springframework.boot.test.autoconfigure.core.AutoConfigureCache;
3233
import org.springframework.boot.test.autoconfigure.filter.TypeExcludeFilters;
3334
import org.springframework.boot.test.json.GsonTester;
@@ -67,6 +68,7 @@
6768
@BootstrapWith(JsonTestContextBootstrapper.class)
6869
@ExtendWith(SpringExtension.class)
6970
@OverrideAutoConfiguration(enabled = false)
71+
@OverrideConfigurationPropertiesScan(enabled = false)
7072
@TypeExcludeFilters(JsonExcludeFilter.class)
7173
@AutoConfigureCache
7274
@AutoConfigureJson

spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/orm/jpa/DataJpaTest.java

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
2929
import org.springframework.boot.autoconfigure.SpringBootApplication;
3030
import org.springframework.boot.test.autoconfigure.OverrideAutoConfiguration;
31+
import org.springframework.boot.test.autoconfigure.OverrideConfigurationPropertiesScan;
3132
import org.springframework.boot.test.autoconfigure.core.AutoConfigureCache;
3233
import org.springframework.boot.test.autoconfigure.filter.TypeExcludeFilters;
3334
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
@@ -72,6 +73,7 @@
7273
@BootstrapWith(DataJpaTestContextBootstrapper.class)
7374
@ExtendWith(SpringExtension.class)
7475
@OverrideAutoConfiguration(enabled = false)
76+
@OverrideConfigurationPropertiesScan(enabled = false)
7577
@TypeExcludeFilters(DataJpaTypeExcludeFilter.class)
7678
@Transactional
7779
@AutoConfigureCache

spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/client/RestClientTest.java

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
2929
import org.springframework.boot.autoconfigure.SpringBootApplication;
3030
import org.springframework.boot.test.autoconfigure.OverrideAutoConfiguration;
31+
import org.springframework.boot.test.autoconfigure.OverrideConfigurationPropertiesScan;
3132
import org.springframework.boot.test.autoconfigure.core.AutoConfigureCache;
3233
import org.springframework.boot.test.autoconfigure.filter.TypeExcludeFilters;
3334
import org.springframework.boot.web.client.RestTemplateBuilder;
@@ -70,6 +71,7 @@
7071
@BootstrapWith(RestClientTestContextBootstrapper.class)
7172
@ExtendWith(SpringExtension.class)
7273
@OverrideAutoConfiguration(enabled = false)
74+
@OverrideConfigurationPropertiesScan(enabled = false)
7375
@TypeExcludeFilters(RestClientExcludeFilter.class)
7476
@AutoConfigureCache
7577
@AutoConfigureWebClient

spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/reactive/WebFluxTest.java

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
2929
import org.springframework.boot.autoconfigure.SpringBootApplication;
3030
import org.springframework.boot.test.autoconfigure.OverrideAutoConfiguration;
31+
import org.springframework.boot.test.autoconfigure.OverrideConfigurationPropertiesScan;
3132
import org.springframework.boot.test.autoconfigure.core.AutoConfigureCache;
3233
import org.springframework.boot.test.autoconfigure.filter.TypeExcludeFilters;
3334
import org.springframework.boot.test.autoconfigure.json.AutoConfigureJson;
@@ -78,6 +79,7 @@
7879
@BootstrapWith(WebFluxTestContextBootstrapper.class)
7980
@ExtendWith(SpringExtension.class)
8081
@OverrideAutoConfiguration(enabled = false)
82+
@OverrideConfigurationPropertiesScan(enabled = false)
8183
@TypeExcludeFilters(WebFluxTypeExcludeFilter.class)
8284
@AutoConfigureCache
8385
@AutoConfigureJson

spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTest.java

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
2929
import org.springframework.boot.autoconfigure.SpringBootApplication;
3030
import org.springframework.boot.test.autoconfigure.OverrideAutoConfiguration;
31+
import org.springframework.boot.test.autoconfigure.OverrideConfigurationPropertiesScan;
3132
import org.springframework.boot.test.autoconfigure.core.AutoConfigureCache;
3233
import org.springframework.boot.test.autoconfigure.filter.TypeExcludeFilters;
3334
import org.springframework.boot.test.context.SpringBootTest;
@@ -79,6 +80,7 @@
7980
@BootstrapWith(WebMvcTestContextBootstrapper.class)
8081
@ExtendWith(SpringExtension.class)
8182
@OverrideAutoConfiguration(enabled = false)
83+
@OverrideConfigurationPropertiesScan(enabled = false)
8284
@TypeExcludeFilters(WebMvcTypeExcludeFilter.class)
8385
@AutoConfigureCache
8486
@AutoConfigureWebMvc

spring-boot-project/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring.factories

+1
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExe
160160
# Spring Test ContextCustomizerFactories
161161
org.springframework.test.context.ContextCustomizerFactory=\
162162
org.springframework.boot.test.autoconfigure.OverrideAutoConfigurationContextCustomizerFactory,\
163+
org.springframework.boot.test.autoconfigure.OverrideConfigurationPropertiesScanContextCustomizerFactory,\
163164
org.springframework.boot.test.autoconfigure.filter.TypeExcludeFiltersContextCustomizerFactory,\
164165
org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizerFactory,\
165166
org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory

0 commit comments

Comments
 (0)