From 3b4ff7d74641bd453554df22bc4eaa55da4522d4 Mon Sep 17 00:00:00 2001 From: Madhura Bhave Date: Fri, 10 May 2019 08:38:10 -0700 Subject: [PATCH] Disable ConfigurationProperties scanning for slice tests Closes gh-16659 --- .../OverrideConfigurationPropertiesScan.java | 48 ++++++++++ ...ropertiesScanContextCustomizerFactory.java | 76 ++++++++++++++++ .../autoconfigure/data/jdbc/DataJdbcTest.java | 2 + .../autoconfigure/data/ldap/DataLdapTest.java | 2 + .../data/mongo/DataMongoTest.java | 2 + .../data/neo4j/DataNeo4jTest.java | 2 + .../data/redis/DataRedisTest.java | 2 + .../test/autoconfigure/jdbc/JdbcTest.java | 2 + .../test/autoconfigure/jooq/JooqTest.java | 2 + .../test/autoconfigure/json/JsonTest.java | 2 + .../autoconfigure/orm/jpa/DataJpaTest.java | 2 + .../web/client/RestClientTest.java | 2 + .../web/reactive/WebFluxTest.java | 2 + .../autoconfigure/web/servlet/WebMvcTest.java | 2 + .../main/resources/META-INF/spring.factories | 1 + ...tiesScanContextCustomizerFactoryTests.java | 90 +++++++++++++++++++ ...figurationPropertiesScanDisabledTests.java | 48 ++++++++++ .../data/jdbc/ExampleProperties.java | 30 +++++++ ...figurationPropertiesScanDisabledTests.java | 48 ++++++++++ .../data/ldap/ExampleProperties.java | 30 +++++++ ...figurationPropertiesScanDisabledTests.java | 48 ++++++++++ .../data/mongo/ExampleProperties.java | 30 +++++++ ...figurationPropertiesScanDisabledTests.java | 73 +++++++++++++++ .../data/neo4j/ExampleProperties.java | 30 +++++++ ...figurationPropertiesScanDisabledTests.java | 48 ++++++++++ .../data/redis/ExampleProperties.java | 30 +++++++ .../autoconfigure/jdbc/ExampleProperties.java | 30 +++++++ ...figurationPropertiesScanDisabledTests.java | 48 ++++++++++ .../autoconfigure/jooq/ExampleProperties.java | 30 +++++++ ...figurationPropertiesScanDisabledTests.java | 48 ++++++++++ ...figurationPropertiesScanDisabledTests.java | 52 +++++++++++ .../json/app/ExampleProperties.java | 31 +++++++ ...figurationPropertiesScanDisabledTests.java | 48 ++++++++++ .../orm/jpa/ExampleProperties.java | 30 +++++++ .../override/ExampleTestProperties.java | 31 +++++++ .../override/scan/ExampleProperties.java | 31 +++++++ ...onfigurationPropertiesScanApplication.java | 31 +++++++ ...rtiesScanEnabledFalseIntegrationTests.java | 67 ++++++++++++++ ...ertiesScanEnabledTrueIntegrationTests.java | 51 +++++++++++ .../web/client/ExampleProperties.java | 30 +++++++ ...figurationPropertiesScanDisabledTests.java | 48 ++++++++++ .../reactive/webclient/ExampleProperties.java | 31 +++++++ ...figurationPropertiesScanDisabledTests.java | 49 ++++++++++ .../servlet/mockmvc/ExampleProperties.java | 31 +++++++ ...figurationPropertiesScanDisabledTests.java | 49 ++++++++++ .../ConfigurationPropertiesScan.java | 5 ++ .../ConfigurationPropertiesScanRegistrar.java | 13 ++- ...igurationPropertiesScanRegistrarTests.java | 19 +++- 48 files changed, 1454 insertions(+), 3 deletions(-) create mode 100644 spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/OverrideConfigurationPropertiesScan.java create mode 100644 spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/OverrideConfigurationPropertiesScanContextCustomizerFactory.java create mode 100644 spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/OverrideConfigurationPropertiesScanContextCustomizerFactoryTests.java create mode 100644 spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/jdbc/DataJdbcTestConfigurationPropertiesScanDisabledTests.java create mode 100644 spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/jdbc/ExampleProperties.java create mode 100644 spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/ldap/DataLdapTestConfigurationPropertiesScanDisabledTests.java create mode 100644 spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/ldap/ExampleProperties.java create mode 100644 spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTestConfigurationPropertiesScanDisabledTests.java create mode 100644 spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/ExampleProperties.java create mode 100644 spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/neo4j/DataNeo4jTestConfigurationPropertiesScanDisabledTests.java create mode 100644 spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/neo4j/ExampleProperties.java create mode 100644 spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/DataRedisTestConfigurationPropertiesScanDisabledTests.java create mode 100644 spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/ExampleProperties.java create mode 100644 spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/ExampleProperties.java create mode 100644 spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/JdbcTestConfigurationPropertiesScanDisabledTests.java create mode 100644 spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jooq/ExampleProperties.java create mode 100644 spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jooq/JooqTestConfigurationPropertiesScanDisabledTests.java create mode 100644 spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/json/JsonTestConfigurationPropertiesScanDisabledTests.java create mode 100644 spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/json/app/ExampleProperties.java create mode 100644 spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/DataJpaTestConfigurationPropertiesScanDisabledTests.java create mode 100644 spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/ExampleProperties.java create mode 100644 spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/override/ExampleTestProperties.java create mode 100644 spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/override/scan/ExampleProperties.java create mode 100644 spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/override/scan/OverrideConfigurationPropertiesScanApplication.java create mode 100644 spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/override/scan/OverrideConfigurationPropertiesScanEnabledFalseIntegrationTests.java create mode 100644 spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/override/scan/OverrideConfigurationPropertiesScanEnabledTrueIntegrationTests.java create mode 100644 spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/client/ExampleProperties.java create mode 100644 spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/client/RestClientTestConfigurationPropertiesScanDisabledTests.java create mode 100644 spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/reactive/webclient/ExampleProperties.java create mode 100644 spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/reactive/webclient/WebFluxTestConfigurationPropertiesScanDisabledTests.java create mode 100644 spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/mockmvc/ExampleProperties.java create mode 100644 spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/mockmvc/WebMvcTestConfigurationPropertiesScanDisabledTests.java diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/OverrideConfigurationPropertiesScan.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/OverrideConfigurationPropertiesScan.java new file mode 100644 index 00000000000..faef170e3c4 --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/OverrideConfigurationPropertiesScan.java @@ -0,0 +1,48 @@ +/* + * Copyright 2012-2019 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; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.springframework.boot.context.properties.ConfigurationPropertiesScan; + +/** + * Annotation that can be used to override + * {@link ConfigurationPropertiesScan @ConfigurationPropertiesScan}. + * + * @author Madhura Bhave + * @since 2.2.0 + * @see ConfigurationPropertiesScan#CONFIGURATION_PROPERTIES_SCAN_ENABLED_PROPERTY + */ +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@Documented +public @interface OverrideConfigurationPropertiesScan { + + /** + * The value of the + * {@link ConfigurationPropertiesScan#CONFIGURATION_PROPERTIES_SCAN_ENABLED_PROPERTY + * enabled override property}. + * @return the override value + */ + boolean enabled(); + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/OverrideConfigurationPropertiesScanContextCustomizerFactory.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/OverrideConfigurationPropertiesScanContextCustomizerFactory.java new file mode 100644 index 00000000000..ce4cbdfa5c0 --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/OverrideConfigurationPropertiesScanContextCustomizerFactory.java @@ -0,0 +1,76 @@ +/* + * Copyright 2012-2019 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; + +import java.util.List; + +import org.springframework.boot.context.properties.ConfigurationPropertiesScan; +import org.springframework.boot.test.util.TestPropertyValues; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.core.annotation.MergedAnnotations; +import org.springframework.core.annotation.MergedAnnotations.SearchStrategy; +import org.springframework.test.context.ContextConfigurationAttributes; +import org.springframework.test.context.ContextCustomizer; +import org.springframework.test.context.ContextCustomizerFactory; +import org.springframework.test.context.MergedContextConfiguration; + +/** + * {@link ContextCustomizerFactory} to support + * {@link OverrideConfigurationPropertiesScan @OverrideConfigurationPropertiesScan}. + * + * @author Madhura Bhave + */ +class OverrideConfigurationPropertiesScanContextCustomizerFactory + implements ContextCustomizerFactory { + + @Override + public ContextCustomizer createContextCustomizer(Class testClass, + List configurationAttributes) { + boolean enabled = MergedAnnotations.from(testClass, SearchStrategy.EXHAUSTIVE) + .get(OverrideConfigurationPropertiesScan.class) + .getValue("enabled", Boolean.class).orElse(true); + return !enabled ? new DisableConfigurationPropertiesContextCustomizer() : null; + } + + /** + * {@link ContextCustomizer} to disable configuration properties scanning. + */ + private static class DisableConfigurationPropertiesContextCustomizer + implements ContextCustomizer { + + @Override + public void customizeContext(ConfigurableApplicationContext context, + MergedContextConfiguration mergedConfig) { + TestPropertyValues.of( + ConfigurationPropertiesScan.CONFIGURATION_PROPERTIES_SCAN_ENABLED_PROPERTY + + "=false") + .applyTo(context); + } + + @Override + public boolean equals(Object obj) { + return (obj != null && obj.getClass() == getClass()); + } + + @Override + public int hashCode() { + return getClass().hashCode(); + } + + } + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/jdbc/DataJdbcTest.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/jdbc/DataJdbcTest.java index 63941f28076..f48fe16ad2f 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/jdbc/DataJdbcTest.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/jdbc/DataJdbcTest.java @@ -28,6 +28,7 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.boot.autoconfigure.ImportAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.test.autoconfigure.OverrideAutoConfiguration; +import org.springframework.boot.test.autoconfigure.OverrideConfigurationPropertiesScan; import org.springframework.boot.test.autoconfigure.core.AutoConfigureCache; import org.springframework.boot.test.autoconfigure.filter.TypeExcludeFilters; import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; @@ -55,6 +56,7 @@ import org.springframework.test.context.junit.jupiter.SpringExtension; @BootstrapWith(DataJdbcTestContextBootstrapper.class) @ExtendWith(SpringExtension.class) @OverrideAutoConfiguration(enabled = false) +@OverrideConfigurationPropertiesScan(enabled = false) @TypeExcludeFilters(DataJdbcTypeExcludeFilter.class) @AutoConfigureCache @AutoConfigureDataJdbc diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/ldap/DataLdapTest.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/ldap/DataLdapTest.java index c3d8a59fe14..01a75a833b7 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/ldap/DataLdapTest.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/ldap/DataLdapTest.java @@ -28,6 +28,7 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.boot.autoconfigure.ImportAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.test.autoconfigure.OverrideAutoConfiguration; +import org.springframework.boot.test.autoconfigure.OverrideConfigurationPropertiesScan; import org.springframework.boot.test.autoconfigure.core.AutoConfigureCache; import org.springframework.boot.test.autoconfigure.filter.TypeExcludeFilters; import org.springframework.context.annotation.ComponentScan.Filter; @@ -58,6 +59,7 @@ import org.springframework.test.context.junit.jupiter.SpringExtension; @BootstrapWith(DataLdapTestContextBootstrapper.class) @ExtendWith(SpringExtension.class) @OverrideAutoConfiguration(enabled = false) +@OverrideConfigurationPropertiesScan(enabled = false) @TypeExcludeFilters(DataLdapTypeExcludeFilter.class) @AutoConfigureCache @AutoConfigureDataLdap diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTest.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTest.java index 567c609ff63..5dfcae99d23 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTest.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTest.java @@ -28,6 +28,7 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.boot.autoconfigure.ImportAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.test.autoconfigure.OverrideAutoConfiguration; +import org.springframework.boot.test.autoconfigure.OverrideConfigurationPropertiesScan; import org.springframework.boot.test.autoconfigure.core.AutoConfigureCache; import org.springframework.boot.test.autoconfigure.filter.TypeExcludeFilters; import org.springframework.context.annotation.ComponentScan.Filter; @@ -59,6 +60,7 @@ import org.springframework.test.context.junit.jupiter.SpringExtension; @BootstrapWith(DataMongoTestContextBootstrapper.class) @ExtendWith(SpringExtension.class) @OverrideAutoConfiguration(enabled = false) +@OverrideConfigurationPropertiesScan(enabled = false) @TypeExcludeFilters(DataMongoTypeExcludeFilter.class) @AutoConfigureCache @AutoConfigureDataMongo diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/neo4j/DataNeo4jTest.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/neo4j/DataNeo4jTest.java index 14b3b4386f3..015c35ab401 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/neo4j/DataNeo4jTest.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/neo4j/DataNeo4jTest.java @@ -28,6 +28,7 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.boot.autoconfigure.ImportAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.test.autoconfigure.OverrideAutoConfiguration; +import org.springframework.boot.test.autoconfigure.OverrideConfigurationPropertiesScan; import org.springframework.boot.test.autoconfigure.core.AutoConfigureCache; import org.springframework.boot.test.autoconfigure.filter.TypeExcludeFilters; import org.springframework.context.annotation.ComponentScan.Filter; @@ -61,6 +62,7 @@ import org.springframework.transaction.annotation.Transactional; @BootstrapWith(DataNeo4jTestContextBootstrapper.class) @ExtendWith(SpringExtension.class) @OverrideAutoConfiguration(enabled = false) +@OverrideConfigurationPropertiesScan(enabled = false) @TypeExcludeFilters(DataNeo4jTypeExcludeFilter.class) @Transactional @AutoConfigureCache diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/redis/DataRedisTest.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/redis/DataRedisTest.java index feced0a0320..69a776f527a 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/redis/DataRedisTest.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/redis/DataRedisTest.java @@ -28,6 +28,7 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.boot.autoconfigure.ImportAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.test.autoconfigure.OverrideAutoConfiguration; +import org.springframework.boot.test.autoconfigure.OverrideConfigurationPropertiesScan; import org.springframework.boot.test.autoconfigure.core.AutoConfigureCache; import org.springframework.boot.test.autoconfigure.filter.TypeExcludeFilters; import org.springframework.context.annotation.ComponentScan.Filter; @@ -55,6 +56,7 @@ import org.springframework.test.context.junit.jupiter.SpringExtension; @BootstrapWith(DataRedisTestContextBootstrapper.class) @ExtendWith(SpringExtension.class) @OverrideAutoConfiguration(enabled = false) +@OverrideConfigurationPropertiesScan(enabled = false) @TypeExcludeFilters(DataRedisTypeExcludeFilter.class) @AutoConfigureCache @AutoConfigureDataRedis diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/jdbc/JdbcTest.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/jdbc/JdbcTest.java index 364ff3e3686..4db6465bc58 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/jdbc/JdbcTest.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/jdbc/JdbcTest.java @@ -28,6 +28,7 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.boot.autoconfigure.ImportAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.test.autoconfigure.OverrideAutoConfiguration; +import org.springframework.boot.test.autoconfigure.OverrideConfigurationPropertiesScan; import org.springframework.boot.test.autoconfigure.core.AutoConfigureCache; import org.springframework.boot.test.autoconfigure.filter.TypeExcludeFilters; import org.springframework.boot.test.context.SpringBootTest; @@ -69,6 +70,7 @@ import org.springframework.transaction.annotation.Transactional; @BootstrapWith(JdbcTestContextBootstrapper.class) @ExtendWith(SpringExtension.class) @OverrideAutoConfiguration(enabled = false) +@OverrideConfigurationPropertiesScan(enabled = false) @TypeExcludeFilters(JdbcTypeExcludeFilter.class) @Transactional @AutoConfigureCache diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/jooq/JooqTest.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/jooq/JooqTest.java index c5de7aa4cc2..8bc224bd2fa 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/jooq/JooqTest.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/jooq/JooqTest.java @@ -28,6 +28,7 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.boot.autoconfigure.ImportAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.test.autoconfigure.OverrideAutoConfiguration; +import org.springframework.boot.test.autoconfigure.OverrideConfigurationPropertiesScan; import org.springframework.boot.test.autoconfigure.filter.TypeExcludeFilters; import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; import org.springframework.context.annotation.ComponentScan.Filter; @@ -62,6 +63,7 @@ import org.springframework.transaction.annotation.Transactional; @BootstrapWith(JooqTestContextBootstrapper.class) @ExtendWith(SpringExtension.class) @OverrideAutoConfiguration(enabled = false) +@OverrideConfigurationPropertiesScan(enabled = false) @TypeExcludeFilters(JooqTypeExcludeFilter.class) @Transactional @AutoConfigureJooq diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/json/JsonTest.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/json/JsonTest.java index 818142fff83..a9715353fa2 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/json/JsonTest.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/json/JsonTest.java @@ -28,6 +28,7 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.boot.autoconfigure.ImportAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.test.autoconfigure.OverrideAutoConfiguration; +import org.springframework.boot.test.autoconfigure.OverrideConfigurationPropertiesScan; import org.springframework.boot.test.autoconfigure.core.AutoConfigureCache; import org.springframework.boot.test.autoconfigure.filter.TypeExcludeFilters; import org.springframework.boot.test.json.GsonTester; @@ -67,6 +68,7 @@ import org.springframework.test.context.junit.jupiter.SpringExtension; @BootstrapWith(JsonTestContextBootstrapper.class) @ExtendWith(SpringExtension.class) @OverrideAutoConfiguration(enabled = false) +@OverrideConfigurationPropertiesScan(enabled = false) @TypeExcludeFilters(JsonExcludeFilter.class) @AutoConfigureCache @AutoConfigureJson diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/orm/jpa/DataJpaTest.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/orm/jpa/DataJpaTest.java index 357e4a024f0..c8285bc22b2 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/orm/jpa/DataJpaTest.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/orm/jpa/DataJpaTest.java @@ -28,6 +28,7 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.boot.autoconfigure.ImportAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.test.autoconfigure.OverrideAutoConfiguration; +import org.springframework.boot.test.autoconfigure.OverrideConfigurationPropertiesScan; import org.springframework.boot.test.autoconfigure.core.AutoConfigureCache; import org.springframework.boot.test.autoconfigure.filter.TypeExcludeFilters; import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; @@ -72,6 +73,7 @@ import org.springframework.transaction.annotation.Transactional; @BootstrapWith(DataJpaTestContextBootstrapper.class) @ExtendWith(SpringExtension.class) @OverrideAutoConfiguration(enabled = false) +@OverrideConfigurationPropertiesScan(enabled = false) @TypeExcludeFilters(DataJpaTypeExcludeFilter.class) @Transactional @AutoConfigureCache diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/client/RestClientTest.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/client/RestClientTest.java index c35fa8a4786..1f89e2a9423 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/client/RestClientTest.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/client/RestClientTest.java @@ -28,6 +28,7 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.boot.autoconfigure.ImportAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.test.autoconfigure.OverrideAutoConfiguration; +import org.springframework.boot.test.autoconfigure.OverrideConfigurationPropertiesScan; import org.springframework.boot.test.autoconfigure.core.AutoConfigureCache; import org.springframework.boot.test.autoconfigure.filter.TypeExcludeFilters; import org.springframework.boot.web.client.RestTemplateBuilder; @@ -70,6 +71,7 @@ import org.springframework.web.client.RestTemplate; @BootstrapWith(RestClientTestContextBootstrapper.class) @ExtendWith(SpringExtension.class) @OverrideAutoConfiguration(enabled = false) +@OverrideConfigurationPropertiesScan(enabled = false) @TypeExcludeFilters(RestClientExcludeFilter.class) @AutoConfigureCache @AutoConfigureWebClient diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/reactive/WebFluxTest.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/reactive/WebFluxTest.java index 1b0444648f0..d3708f85b4c 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/reactive/WebFluxTest.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/reactive/WebFluxTest.java @@ -28,6 +28,7 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.boot.autoconfigure.ImportAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.test.autoconfigure.OverrideAutoConfiguration; +import org.springframework.boot.test.autoconfigure.OverrideConfigurationPropertiesScan; import org.springframework.boot.test.autoconfigure.core.AutoConfigureCache; import org.springframework.boot.test.autoconfigure.filter.TypeExcludeFilters; import org.springframework.boot.test.autoconfigure.json.AutoConfigureJson; @@ -78,6 +79,7 @@ import org.springframework.test.web.reactive.server.WebTestClient; @BootstrapWith(WebFluxTestContextBootstrapper.class) @ExtendWith(SpringExtension.class) @OverrideAutoConfiguration(enabled = false) +@OverrideConfigurationPropertiesScan(enabled = false) @TypeExcludeFilters(WebFluxTypeExcludeFilter.class) @AutoConfigureCache @AutoConfigureJson diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTest.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTest.java index 29f3bc7a1f9..2a6e908d5f3 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTest.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTest.java @@ -28,6 +28,7 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.boot.autoconfigure.ImportAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.test.autoconfigure.OverrideAutoConfiguration; +import org.springframework.boot.test.autoconfigure.OverrideConfigurationPropertiesScan; import org.springframework.boot.test.autoconfigure.core.AutoConfigureCache; import org.springframework.boot.test.autoconfigure.filter.TypeExcludeFilters; import org.springframework.boot.test.context.SpringBootTest; @@ -79,6 +80,7 @@ import org.springframework.test.web.servlet.MockMvc; @BootstrapWith(WebMvcTestContextBootstrapper.class) @ExtendWith(SpringExtension.class) @OverrideAutoConfiguration(enabled = false) +@OverrideConfigurationPropertiesScan(enabled = false) @TypeExcludeFilters(WebMvcTypeExcludeFilter.class) @AutoConfigureCache @AutoConfigureWebMvc diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring.factories b/spring-boot-project/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring.factories index d05127f9421..0983f676af2 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring.factories +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring.factories @@ -160,6 +160,7 @@ org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExe # Spring Test ContextCustomizerFactories org.springframework.test.context.ContextCustomizerFactory=\ org.springframework.boot.test.autoconfigure.OverrideAutoConfigurationContextCustomizerFactory,\ +org.springframework.boot.test.autoconfigure.OverrideConfigurationPropertiesScanContextCustomizerFactory,\ org.springframework.boot.test.autoconfigure.filter.TypeExcludeFiltersContextCustomizerFactory,\ org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizerFactory,\ org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/OverrideConfigurationPropertiesScanContextCustomizerFactoryTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/OverrideConfigurationPropertiesScanContextCustomizerFactoryTests.java new file mode 100644 index 00000000000..abe593a35fa --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/OverrideConfigurationPropertiesScanContextCustomizerFactoryTests.java @@ -0,0 +1,90 @@ +/* + * Copyright 2012-2019 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; + +import org.junit.Test; + +import org.springframework.boot.context.properties.ConfigurationPropertiesScan; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.test.context.ContextCustomizer; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Madhura Bhave + */ +public class OverrideConfigurationPropertiesScanContextCustomizerFactoryTests { + + private OverrideConfigurationPropertiesScanContextCustomizerFactory factory = new OverrideConfigurationPropertiesScanContextCustomizerFactory(); + + @Test + public void getContextCustomizerWhenHasNoAnnotationShouldReturnNull() { + ContextCustomizer customizer = this.factory + .createContextCustomizer(NoAnnotation.class, null); + assertThat(customizer).isNull(); + } + + @Test + public void getContextCustomizerWhenHasAnnotationEnabledTrueShouldReturnNull() { + ContextCustomizer customizer = this.factory + .createContextCustomizer(WithAnnotationEnabledTrue.class, null); + assertThat(customizer).isNull(); + } + + @Test + public void getContextCustomizerWhenHasAnnotationEnabledFalseShouldReturnCustomizer() { + ContextCustomizer customizer = this.factory + .createContextCustomizer(WithAnnotationEnabledFalse.class, null); + assertThat(customizer).isNotNull(); + ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(); + customizer.customizeContext(context, null); + String property = context.getEnvironment().getProperty( + ConfigurationPropertiesScan.CONFIGURATION_PROPERTIES_SCAN_ENABLED_PROPERTY); + assertThat(property).isEqualTo("false"); + } + + @Test + public void hashCodeAndEquals() { + ContextCustomizer customizer1 = this.factory + .createContextCustomizer(WithAnnotationEnabledFalse.class, null); + ContextCustomizer customizer2 = this.factory + .createContextCustomizer(WithSameAnnotation.class, null); + assertThat(customizer1.hashCode()).isEqualTo(customizer2.hashCode()); + assertThat(customizer1).isEqualTo(customizer1).isEqualTo(customizer2); + } + + static class NoAnnotation { + + } + + @OverrideConfigurationPropertiesScan(enabled = true) + static class WithAnnotationEnabledTrue { + + } + + @OverrideConfigurationPropertiesScan(enabled = false) + static class WithAnnotationEnabledFalse { + + } + + @OverrideConfigurationPropertiesScan(enabled = false) + static class WithSameAnnotation { + + } + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/jdbc/DataJdbcTestConfigurationPropertiesScanDisabledTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/jdbc/DataJdbcTestConfigurationPropertiesScanDisabledTests.java new file mode 100644 index 00000000000..5f0ca04a8ad --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/jdbc/DataJdbcTestConfigurationPropertiesScanDisabledTests.java @@ -0,0 +1,48 @@ +/* + * Copyright 2012-2019 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 org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.NoSuchBeanDefinitionException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; + +/** + * Tests to ensure that configuration properties scanning is disabled for + * {@link DataJdbcTest @DataJdbcTest}. + * + * @author Madhura Bhave + */ +@RunWith(SpringRunner.class) +@DataJdbcTest +public class DataJdbcTestConfigurationPropertiesScanDisabledTests { + + @Autowired + private ApplicationContext context; + + @Test + public void configurationProperiesScanDisabled() { + assertThatExceptionOfType(NoSuchBeanDefinitionException.class) + .isThrownBy(() -> this.context.getBean(ExampleProperties.class)); + } + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/jdbc/ExampleProperties.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/jdbc/ExampleProperties.java new file mode 100644 index 00000000000..e2a3fc6413d --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/jdbc/ExampleProperties.java @@ -0,0 +1,30 @@ +/* + * Copyright 2012-2019 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 org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * Example {@link ConfigurationProperties @ConfigurationProperties} for use with + * {@link DataJdbcTest @DataJdbcTest} tests. + * + * @author Madhura Bhave + */ +@ConfigurationProperties(prefix = "example") +public class ExampleProperties { + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/ldap/DataLdapTestConfigurationPropertiesScanDisabledTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/ldap/DataLdapTestConfigurationPropertiesScanDisabledTests.java new file mode 100644 index 00000000000..d7590397ff9 --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/ldap/DataLdapTestConfigurationPropertiesScanDisabledTests.java @@ -0,0 +1,48 @@ +/* + * Copyright 2012-2019 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.ldap; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.NoSuchBeanDefinitionException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; + +/** + * Tests to ensure that configuration properties scanning is disabled for + * {@link DataLdapTest @DataLdapTest}. + * + * @author Madhura Bhave + */ +@RunWith(SpringRunner.class) +@DataLdapTest +public class DataLdapTestConfigurationPropertiesScanDisabledTests { + + @Autowired + private ApplicationContext context; + + @Test + public void configurationProperiesScanDisabled() { + assertThatExceptionOfType(NoSuchBeanDefinitionException.class) + .isThrownBy(() -> this.context.getBean(ExampleProperties.class)); + } + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/ldap/ExampleProperties.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/ldap/ExampleProperties.java new file mode 100644 index 00000000000..a253dbb296b --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/ldap/ExampleProperties.java @@ -0,0 +1,30 @@ +/* + * Copyright 2012-2019 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.ldap; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * Example {@link ConfigurationProperties @ConfigurationProperties} for use with + * {@link DataLdapTest @DataLdapTest} tests. + * + * @author Madhura Bhave + */ +@ConfigurationProperties(prefix = "example") +public class ExampleProperties { + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTestConfigurationPropertiesScanDisabledTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTestConfigurationPropertiesScanDisabledTests.java new file mode 100644 index 00000000000..bed0b832cff --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/DataMongoTestConfigurationPropertiesScanDisabledTests.java @@ -0,0 +1,48 @@ +/* + * Copyright 2012-2019 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.mongo; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.NoSuchBeanDefinitionException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; + +/** + * Tests to ensure that configuration properties scanning is disabled for + * {@link DataMongoTest @DataMongoTest}. + * + * @author Madhura Bhave + */ +@RunWith(SpringRunner.class) +@DataMongoTest +public class DataMongoTestConfigurationPropertiesScanDisabledTests { + + @Autowired + private ApplicationContext context; + + @Test + public void configurationProperiesScanDisabled() { + assertThatExceptionOfType(NoSuchBeanDefinitionException.class) + .isThrownBy(() -> this.context.getBean(ExampleProperties.class)); + } + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/ExampleProperties.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/ExampleProperties.java new file mode 100644 index 00000000000..80750e8c673 --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/mongo/ExampleProperties.java @@ -0,0 +1,30 @@ +/* + * Copyright 2012-2019 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.mongo; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * Example {@link ConfigurationProperties @ConfigurationProperties} for use with + * {@link DataMongoTest @DataMongoTest} tests. + * + * @author Madhura Bhave + */ +@ConfigurationProperties(prefix = "example") +public class ExampleProperties { + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/neo4j/DataNeo4jTestConfigurationPropertiesScanDisabledTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/neo4j/DataNeo4jTestConfigurationPropertiesScanDisabledTests.java new file mode 100644 index 00000000000..ecd7498d79e --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/neo4j/DataNeo4jTestConfigurationPropertiesScanDisabledTests.java @@ -0,0 +1,73 @@ +/* + * Copyright 2012-2019 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.neo4j; + +import org.junit.jupiter.api.Test; +import org.testcontainers.containers.Neo4jContainer; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; + +import org.springframework.beans.factory.NoSuchBeanDefinitionException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.util.TestPropertyValues; +import org.springframework.boot.testsupport.testcontainers.SkippableContainer; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextInitializer; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.test.context.ContextConfiguration; + +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; + +/** + * Tests to ensure that configuration properties scanning is disabled for + * {@link DataNeo4jTest @DataNeo4jTest}. + * + * @author Madhura Bhave + */ +@ContextConfiguration( + initializers = DataNeo4jTestConfigurationPropertiesScanDisabledTests.Initializer.class) +@Testcontainers +@DataNeo4jTest +public class DataNeo4jTestConfigurationPropertiesScanDisabledTests { + + @Container + public static SkippableContainer> neo4j = new SkippableContainer<>( + () -> new Neo4jContainer<>().withAdminPassword(null)); + + @Autowired + private ApplicationContext context; + + @Test + public void configurationProperiesScanDisabled() { + assertThatExceptionOfType(NoSuchBeanDefinitionException.class) + .isThrownBy(() -> this.context.getBean(ExampleProperties.class)); + } + + static class Initializer + implements ApplicationContextInitializer { + + @Override + public void initialize( + ConfigurableApplicationContext configurableApplicationContext) { + TestPropertyValues + .of("spring.data.neo4j.uri=" + neo4j.getContainer().getBoltUrl()) + .applyTo(configurableApplicationContext.getEnvironment()); + } + + } + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/neo4j/ExampleProperties.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/neo4j/ExampleProperties.java new file mode 100644 index 00000000000..2c35c336820 --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/neo4j/ExampleProperties.java @@ -0,0 +1,30 @@ +/* + * Copyright 2012-2019 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.neo4j; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * Example {@link ConfigurationProperties @ConfigurationProperties} for use with + * {@link DataNeo4jTest @DataNeo4jTest} tests. + * + * @author Madhura Bhave + */ +@ConfigurationProperties(prefix = "example") +public class ExampleProperties { + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/DataRedisTestConfigurationPropertiesScanDisabledTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/DataRedisTestConfigurationPropertiesScanDisabledTests.java new file mode 100644 index 00000000000..f241a9a9755 --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/DataRedisTestConfigurationPropertiesScanDisabledTests.java @@ -0,0 +1,48 @@ +/* + * Copyright 2012-2019 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.redis; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.NoSuchBeanDefinitionException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; + +/** + * Tests to ensure that configuration properties scanning is disabled for + * {@link DataRedisTest @DataRedisTest}. + * + * @author Madhura Bhave + */ +@RunWith(SpringRunner.class) +@DataRedisTest +public class DataRedisTestConfigurationPropertiesScanDisabledTests { + + @Autowired + private ApplicationContext context; + + @Test + public void configurationProperiesScanDisabled() { + assertThatExceptionOfType(NoSuchBeanDefinitionException.class) + .isThrownBy(() -> this.context.getBean(ExampleProperties.class)); + } + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/ExampleProperties.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/ExampleProperties.java new file mode 100644 index 00000000000..4905a2fa02b --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/redis/ExampleProperties.java @@ -0,0 +1,30 @@ +/* + * Copyright 2012-2019 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.redis; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * Example {@link ConfigurationProperties @ConfigurationProperties} for use with + * {@link DataRedisTest @DataRedisTest} tests. + * + * @author Madhura Bhave + */ +@ConfigurationProperties(prefix = "example") +public class ExampleProperties { + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/ExampleProperties.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/ExampleProperties.java new file mode 100644 index 00000000000..3b48f57722d --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/ExampleProperties.java @@ -0,0 +1,30 @@ +/* + * Copyright 2012-2019 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.jdbc; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * Example {@link ConfigurationProperties @ConfigurationProperties} for use with + * {@link JdbcTest @JdbcTest} tests. + * + * @author Madhura Bhave + */ +@ConfigurationProperties(prefix = "example") +public class ExampleProperties { + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/JdbcTestConfigurationPropertiesScanDisabledTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/JdbcTestConfigurationPropertiesScanDisabledTests.java new file mode 100644 index 00000000000..fcb0d156fa5 --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jdbc/JdbcTestConfigurationPropertiesScanDisabledTests.java @@ -0,0 +1,48 @@ +/* + * Copyright 2012-2019 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.jdbc; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.NoSuchBeanDefinitionException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; + +/** + * Tests to ensure that configuration properties scanning is disabled for + * {@link JdbcTest @JdbcTest}. + * + * @author Madhura Bhave + */ +@RunWith(SpringRunner.class) +@JdbcTest +public class JdbcTestConfigurationPropertiesScanDisabledTests { + + @Autowired + private ApplicationContext context; + + @Test + public void configurationProperiesScanDisabled() { + assertThatExceptionOfType(NoSuchBeanDefinitionException.class) + .isThrownBy(() -> this.context.getBean(ExampleProperties.class)); + } + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jooq/ExampleProperties.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jooq/ExampleProperties.java new file mode 100644 index 00000000000..45bc850ccaf --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jooq/ExampleProperties.java @@ -0,0 +1,30 @@ +/* + * Copyright 2012-2019 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.jooq; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * Example {@link ConfigurationProperties @ConfigurationProperties} for use with + * {@link JooqTest @JooqTest} tests. + * + * @author Madhura Bhave + */ +@ConfigurationProperties(prefix = "example") +public class ExampleProperties { + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jooq/JooqTestConfigurationPropertiesScanDisabledTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jooq/JooqTestConfigurationPropertiesScanDisabledTests.java new file mode 100644 index 00000000000..fc28f6b29a6 --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/jooq/JooqTestConfigurationPropertiesScanDisabledTests.java @@ -0,0 +1,48 @@ +/* + * Copyright 2012-2019 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.jooq; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.NoSuchBeanDefinitionException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; + +/** + * Tests to ensure that configuration properties scanning is disabled for + * {@link JooqTest @JooqTest}. + * + * @author Madhura Bhave + */ +@RunWith(SpringRunner.class) +@JooqTest +public class JooqTestConfigurationPropertiesScanDisabledTests { + + @Autowired + private ApplicationContext context; + + @Test + public void configurationProperiesScanDisabled() { + assertThatExceptionOfType(NoSuchBeanDefinitionException.class) + .isThrownBy(() -> this.context.getBean(ExampleProperties.class)); + } + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/json/JsonTestConfigurationPropertiesScanDisabledTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/json/JsonTestConfigurationPropertiesScanDisabledTests.java new file mode 100644 index 00000000000..999b7fc9b2b --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/json/JsonTestConfigurationPropertiesScanDisabledTests.java @@ -0,0 +1,52 @@ +/* + * Copyright 2012-2019 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.json; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.NoSuchBeanDefinitionException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.json.app.ExampleJsonApplication; +import org.springframework.boot.test.autoconfigure.json.app.ExampleProperties; +import org.springframework.context.ApplicationContext; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; + +/** + * Tests to ensure that configuration properties scanning is disabled for + * {@link JsonTest @JsonTest}. + * + * @author Madhura Bhave + */ +@RunWith(SpringRunner.class) +@JsonTest +@ContextConfiguration(classes = ExampleJsonApplication.class) +public class JsonTestConfigurationPropertiesScanDisabledTests { + + @Autowired + private ApplicationContext context; + + @Test + public void configurationProperiesScanDisabled() { + assertThatExceptionOfType(NoSuchBeanDefinitionException.class) + .isThrownBy(() -> this.context.getBean(ExampleProperties.class)); + } + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/json/app/ExampleProperties.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/json/app/ExampleProperties.java new file mode 100644 index 00000000000..aa03dfd54a5 --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/json/app/ExampleProperties.java @@ -0,0 +1,31 @@ +/* + * Copyright 2012-2019 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.json.app; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.test.autoconfigure.json.JsonTest; + +/** + * Example {@link ConfigurationProperties @ConfigurationProperties} for use with + * {@link JsonTest @JsonTest} tests. + * + * @author Madhura Bhave + */ +@ConfigurationProperties(prefix = "example") +public class ExampleProperties { + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/DataJpaTestConfigurationPropertiesScanDisabledTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/DataJpaTestConfigurationPropertiesScanDisabledTests.java new file mode 100644 index 00000000000..815401f1808 --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/DataJpaTestConfigurationPropertiesScanDisabledTests.java @@ -0,0 +1,48 @@ +/* + * Copyright 2012-2019 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.orm.jpa; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.NoSuchBeanDefinitionException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; + +/** + * Tests to ensure that configuration properties scanning is disabled for + * {@link DataJpaTest @DataJpaTest}. + * + * @author Madhura Bhave + */ +@RunWith(SpringRunner.class) +@DataJpaTest +public class DataJpaTestConfigurationPropertiesScanDisabledTests { + + @Autowired + private ApplicationContext context; + + @Test + public void configurationProperiesScanDisabled() { + assertThatExceptionOfType(NoSuchBeanDefinitionException.class) + .isThrownBy(() -> this.context.getBean(ExampleProperties.class)); + } + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/ExampleProperties.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/ExampleProperties.java new file mode 100644 index 00000000000..4607a8113a8 --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/orm/jpa/ExampleProperties.java @@ -0,0 +1,30 @@ +/* + * Copyright 2012-2019 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.orm.jpa; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * Example {@link ConfigurationProperties @ConfigurationProperties} for use with + * {@link DataJpaTest @DataJpaTest} tests. + * + * @author Madhura Bhave + */ +@ConfigurationProperties(prefix = "example") +public class ExampleProperties { + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/override/ExampleTestProperties.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/override/ExampleTestProperties.java new file mode 100644 index 00000000000..0b78bc229ee --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/override/ExampleTestProperties.java @@ -0,0 +1,31 @@ +/* + * Copyright 2012-2019 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.override; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.test.autoconfigure.OverrideConfigurationPropertiesScan; + +/** + * Example {@link ConfigurationProperties @ConfigurationProperties} for use with + * {@link OverrideConfigurationPropertiesScan @OverrideConfigurationPropertiesScan} tests. + * + * @author Madhura Bhave + */ +@ConfigurationProperties(prefix = "example") +public class ExampleTestProperties { + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/override/scan/ExampleProperties.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/override/scan/ExampleProperties.java new file mode 100644 index 00000000000..35bd021e255 --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/override/scan/ExampleProperties.java @@ -0,0 +1,31 @@ +/* + * Copyright 2012-2019 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.override.scan; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.test.autoconfigure.OverrideConfigurationPropertiesScan; + +/** + * Example {@link ConfigurationProperties @ConfigurationProperties} for use with + * {@link OverrideConfigurationPropertiesScan @OverrideConfigurationPropertiesScan} tests. + * + * @author Madhura Bhave + */ +@ConfigurationProperties(prefix = "example") +public class ExampleProperties { + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/override/scan/OverrideConfigurationPropertiesScanApplication.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/override/scan/OverrideConfigurationPropertiesScanApplication.java new file mode 100644 index 00000000000..b1293c0f225 --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/override/scan/OverrideConfigurationPropertiesScanApplication.java @@ -0,0 +1,31 @@ +/* + * Copyright 2012-2019 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.override.scan; + +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.test.autoconfigure.OverrideConfigurationPropertiesScan; + +/** + * Example {@link SpringBootApplication @SpringBootApplication} for use with + * {@link OverrideConfigurationPropertiesScan @OverrideConfigurationPropertiesScan} tests. + * + * @author Madhura Bhave + */ +@SpringBootApplication +public class OverrideConfigurationPropertiesScanApplication { + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/override/scan/OverrideConfigurationPropertiesScanEnabledFalseIntegrationTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/override/scan/OverrideConfigurationPropertiesScanEnabledFalseIntegrationTests.java new file mode 100644 index 00000000000..c88f51ac05d --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/override/scan/OverrideConfigurationPropertiesScanEnabledFalseIntegrationTests.java @@ -0,0 +1,67 @@ +/* + * Copyright 2012-2019 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.override.scan; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.NoSuchBeanDefinitionException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.boot.test.autoconfigure.OverrideConfigurationPropertiesScan; +import org.springframework.boot.test.autoconfigure.override.ExampleTestProperties; +import org.springframework.boot.test.context.SpringBootTestContextBootstrapper; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.test.context.BootstrapWith; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; + +/** + * Integration tests for + * {@link OverrideConfigurationPropertiesScan @OverrideConfigurationPropertiesScan} when + * {@code enabled} is {@code false}. + * + * @author Madhura Bhave + */ +@RunWith(SpringRunner.class) +@BootstrapWith(SpringBootTestContextBootstrapper.class) +@OverrideConfigurationPropertiesScan(enabled = false) +@Import(OverrideConfigurationPropertiesScanEnabledFalseIntegrationTests.TestConfiguration.class) +public class OverrideConfigurationPropertiesScanEnabledFalseIntegrationTests { + + @Autowired + private ApplicationContext context; + + @Test + public void disabledConfigurationPropertiesScan() { + ApplicationContext context = this.context; + assertThat(context.getBean(ExampleTestProperties.class)).isNotNull(); + assertThatExceptionOfType(NoSuchBeanDefinitionException.class) + .isThrownBy(() -> context.getBean(ExampleProperties.class)); + } + + @Configuration + @EnableConfigurationProperties(ExampleTestProperties.class) + static class TestConfiguration { + + } + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/override/scan/OverrideConfigurationPropertiesScanEnabledTrueIntegrationTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/override/scan/OverrideConfigurationPropertiesScanEnabledTrueIntegrationTests.java new file mode 100644 index 00000000000..a18544a4bef --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/override/scan/OverrideConfigurationPropertiesScanEnabledTrueIntegrationTests.java @@ -0,0 +1,51 @@ +/* + * Copyright 2012-2019 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.override.scan; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.OverrideConfigurationPropertiesScan; +import org.springframework.boot.test.context.SpringBootTestContextBootstrapper; +import org.springframework.context.ApplicationContext; +import org.springframework.test.context.BootstrapWith; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Integration tests for + * {@link OverrideConfigurationPropertiesScan @OverrideConfigurationPropertiesScan} when + * {@code enabled} is {@code true}. + * + * @author Madhura Bhave + */ +@RunWith(SpringRunner.class) +@OverrideConfigurationPropertiesScan(enabled = true) +@BootstrapWith(SpringBootTestContextBootstrapper.class) +public class OverrideConfigurationPropertiesScanEnabledTrueIntegrationTests { + + @Autowired + private ApplicationContext context; + + @Test + public void configurationPropertiesScanEnabled() { + assertThat(this.context.getBean(ExampleProperties.class)).isNotNull(); + } + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/client/ExampleProperties.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/client/ExampleProperties.java new file mode 100644 index 00000000000..554b8ebef12 --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/client/ExampleProperties.java @@ -0,0 +1,30 @@ +/* + * Copyright 2012-2019 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.web.client; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * Example {@link ConfigurationProperties @ConfigurationProperties} for use with + * {@link RestClientTest @RestClientTest} tests. + * + * @author Madhura Bhave + */ +@ConfigurationProperties(prefix = "example") +public class ExampleProperties { + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/client/RestClientTestConfigurationPropertiesScanDisabledTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/client/RestClientTestConfigurationPropertiesScanDisabledTests.java new file mode 100644 index 00000000000..1f4fd4bd6a9 --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/client/RestClientTestConfigurationPropertiesScanDisabledTests.java @@ -0,0 +1,48 @@ +/* + * Copyright 2012-2019 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.web.client; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.NoSuchBeanDefinitionException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; + +/** + * Tests to ensure that configuration properties scanning is disabled for + * {@link RestClientTest @RestClientTest}. + * + * @author Madhura Bhave + */ +@RunWith(SpringRunner.class) +@RestClientTest +public class RestClientTestConfigurationPropertiesScanDisabledTests { + + @Autowired + private ApplicationContext context; + + @Test + public void configurationProperiesScanDisabled() { + assertThatExceptionOfType(NoSuchBeanDefinitionException.class) + .isThrownBy(() -> this.context.getBean(ExampleProperties.class)); + } + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/reactive/webclient/ExampleProperties.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/reactive/webclient/ExampleProperties.java new file mode 100644 index 00000000000..90d6a959bf5 --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/reactive/webclient/ExampleProperties.java @@ -0,0 +1,31 @@ +/* + * Copyright 2012-2019 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.web.reactive.webclient; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest; + +/** + * Example {@link ConfigurationProperties @ConfigurationProperties} for use with + * {@link WebFluxTest @WebFluxTest} tests. + * + * @author Madhura Bhave + */ +@ConfigurationProperties(prefix = "example") +public class ExampleProperties { + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/reactive/webclient/WebFluxTestConfigurationPropertiesScanDisabledTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/reactive/webclient/WebFluxTestConfigurationPropertiesScanDisabledTests.java new file mode 100644 index 00000000000..27d7e78aa38 --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/reactive/webclient/WebFluxTestConfigurationPropertiesScanDisabledTests.java @@ -0,0 +1,49 @@ +/* + * Copyright 2012-2019 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.web.reactive.webclient; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.NoSuchBeanDefinitionException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest; +import org.springframework.context.ApplicationContext; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; + +/** + * Tests to ensure that configuration properties scanning is disabled for + * {@link WebFluxTest @WebFluxTest}. + * + * @author Madhura Bhave + */ +@RunWith(SpringRunner.class) +@WebFluxTest +public class WebFluxTestConfigurationPropertiesScanDisabledTests { + + @Autowired + private ApplicationContext context; + + @Test + public void configurationProperiesScanDisabled() { + assertThatExceptionOfType(NoSuchBeanDefinitionException.class) + .isThrownBy(() -> this.context.getBean(ExampleProperties.class)); + } + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/mockmvc/ExampleProperties.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/mockmvc/ExampleProperties.java new file mode 100644 index 00000000000..dd45db0b933 --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/mockmvc/ExampleProperties.java @@ -0,0 +1,31 @@ +/* + * Copyright 2012-2019 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.web.servlet.mockmvc; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; + +/** + * Example {@link ConfigurationProperties @ConfigurationProperties} for use with + * {@link WebMvcTest @WebMvcTest} tests. + * + * @author Madhura Bhave + */ +@ConfigurationProperties(prefix = "example") +public class ExampleProperties { + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/mockmvc/WebMvcTestConfigurationPropertiesScanDisabledTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/mockmvc/WebMvcTestConfigurationPropertiesScanDisabledTests.java new file mode 100644 index 00000000000..c53b0ba6829 --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/mockmvc/WebMvcTestConfigurationPropertiesScanDisabledTests.java @@ -0,0 +1,49 @@ +/* + * Copyright 2012-2019 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.web.servlet.mockmvc; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.NoSuchBeanDefinitionException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.context.ApplicationContext; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; + +/** + * Tests to ensure that configuration properties scanning is disabled for + * {@link WebMvcTest @WebMvcTest}. + * + * @author Madhura Bhave + */ +@RunWith(SpringRunner.class) +@WebMvcTest +public class WebMvcTestConfigurationPropertiesScanDisabledTests { + + @Autowired + private ApplicationContext context; + + @Test + public void configurationProperiesScanDisabled() { + assertThatExceptionOfType(NoSuchBeanDefinitionException.class) + .isThrownBy(() -> this.context.getBean(ExampleProperties.class)); + } + +} diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesScan.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesScan.java index c4d7ef4b632..1cee3c4f60a 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesScan.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesScan.java @@ -40,6 +40,11 @@ import org.springframework.core.annotation.AliasFor; @Import(ConfigurationPropertiesScanRegistrar.class) public @interface ConfigurationPropertiesScan { + /** + * Whether configuration properties should be automatically scanned. + */ + String CONFIGURATION_PROPERTIES_SCAN_ENABLED_PROPERTY = "spring.boot.configurationpropertiesscan.enabled"; + /** * Alias for the {@link #basePackages()} attribute. Allows for more concise annotation * declarations e.g.: {@code @ConfigurationPropertiesScan("org.my.pkg")} instead of diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesScanRegistrar.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesScanRegistrar.java index 0e1c45da36a..1867efbbc0c 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesScanRegistrar.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesScanRegistrar.java @@ -53,8 +53,17 @@ class ConfigurationPropertiesScanRegistrar @Override public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { - Set packagesToScan = getPackagesToScan(importingClassMetadata); - register(registry, (ConfigurableListableBeanFactory) registry, packagesToScan); + if (isEnabled()) { + Set packagesToScan = getPackagesToScan(importingClassMetadata); + register(registry, (ConfigurableListableBeanFactory) registry, + packagesToScan); + } + } + + protected boolean isEnabled() { + return this.environment.getProperty( + ConfigurationPropertiesScan.CONFIGURATION_PROPERTIES_SCAN_ENABLED_PROPERTY, + Boolean.class, true); } private Set getPackagesToScan(AnnotationMetadata metadata) { diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesScanRegistrarTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesScanRegistrarTests.java index cc76cc8176b..3ac41b49051 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesScanRegistrarTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesScanRegistrarTests.java @@ -20,6 +20,7 @@ import java.io.IOException; import org.junit.Before; import org.junit.Test; +import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.GenericBeanDefinition; @@ -44,9 +45,25 @@ public class ConfigurationPropertiesScanRegistrarTests { private final DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); + private MockEnvironment environment; + @Before public void setup() { - this.registrar.setEnvironment(new MockEnvironment()); + this.environment = new MockEnvironment(); + this.registrar.setEnvironment(this.environment); + } + + @Test + public void scanningDisabled() throws IOException { + this.environment.setProperty( + ConfigurationPropertiesScan.CONFIGURATION_PROPERTIES_SCAN_ENABLED_PROPERTY, + "false"); + this.registrar.registerBeanDefinitions( + getAnnotationMetadata(ConfigurationPropertiesScanConfiguration.class), + this.beanFactory); + assertThatExceptionOfType(NoSuchBeanDefinitionException.class) + .isThrownBy(() -> this.beanFactory.getBeanDefinition( + "bing-org.springframework.boot.context.properties.scan.valid.ConfigurationPropertiesScanConfiguration$BingProperties")); } @Test