diff --git a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootContextLoader.java b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootContextLoader.java index 01694563c0..148c2f41e7 100644 --- a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootContextLoader.java +++ b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootContextLoader.java @@ -42,7 +42,6 @@ import org.springframework.core.annotation.MergedAnnotations; import org.springframework.core.annotation.MergedAnnotations.SearchStrategy; import org.springframework.core.annotation.Order; import org.springframework.core.env.ConfigurableEnvironment; -import org.springframework.core.env.StandardEnvironment; import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.ResourceLoader; import org.springframework.test.context.ContextConfigurationAttributes; @@ -55,7 +54,6 @@ import org.springframework.test.context.support.TestPropertySourceUtils; import org.springframework.test.context.web.WebMergedContextConfiguration; import org.springframework.util.Assert; import org.springframework.util.ObjectUtils; -import org.springframework.util.ReflectionUtils; import org.springframework.util.StringUtils; import org.springframework.web.context.support.GenericWebApplicationContext; @@ -114,10 +112,8 @@ public class SpringBootContextLoader extends AbstractContextLoader { application.setWebApplicationType(WebApplicationType.NONE); } application.setInitializers(initializers); - boolean customEnvironent = ReflectionUtils.findMethod(getClass(), "getEnvironment") - .getDeclaringClass() != SpringBootContextLoader.class; - if (customEnvironent) { - ConfigurableEnvironment environment = getEnvironment(); + ConfigurableEnvironment environment = getEnvironment(); + if (environment != null) { prepareEnvironment(config, application, environment, false); application.setEnvironment(environment); } @@ -163,12 +159,13 @@ public class SpringBootContextLoader extends AbstractContextLoader { } /** - * Builds a new {@link ConfigurableEnvironment} instance. You can override this method - * to return something other than {@link StandardEnvironment} if necessary. + * Returns the {@link ConfigurableEnvironment} instance that should be applied to + * {@link SpringApplication} or {@code null} to use the default. You can override this + * method if you need a custom environment. * @return a {@link ConfigurableEnvironment} instance */ protected ConfigurableEnvironment getEnvironment() { - return new StandardEnvironment(); + return null; } protected String[] getInlinedProperties(MergedContextConfiguration config) { diff --git a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringBootTestWithActiveProfilesAndEnvironmentPropertyTests.java b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringBootTestWithActiveProfilesAndEnvironmentPropertyTests.java index 19ff569e93..ecad63b7b0 100644 --- a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringBootTestWithActiveProfilesAndEnvironmentPropertyTests.java +++ b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringBootTestWithActiveProfilesAndEnvironmentPropertyTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2021 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,6 +27,7 @@ import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.Environment; import org.springframework.core.env.MapPropertySource; import org.springframework.core.env.MutablePropertySources; +import org.springframework.core.env.StandardEnvironment; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ContextConfiguration; @@ -61,7 +62,7 @@ class SpringBootTestWithActiveProfilesAndEnvironmentPropertyTests { @Override protected ConfigurableEnvironment getEnvironment() { - ConfigurableEnvironment environment = super.getEnvironment(); + ConfigurableEnvironment environment = new StandardEnvironment(); MutablePropertySources sources = environment.getPropertySources(); Map map = new LinkedHashMap<>(); map.put("spring.profiles.active", "local"); diff --git a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringBootTestWithActiveProfilesAndSystemEnvironmentPropertyTests.java b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringBootTestWithActiveProfilesAndSystemEnvironmentPropertyTests.java index 66b54c2caf..02c0c83d10 100644 --- a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringBootTestWithActiveProfilesAndSystemEnvironmentPropertyTests.java +++ b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringBootTestWithActiveProfilesAndSystemEnvironmentPropertyTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2021 the original author or authors. + * Copyright 2012-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -64,7 +64,7 @@ class SpringBootTestWithActiveProfilesAndSystemEnvironmentPropertyTests { @Override @SuppressWarnings("unchecked") protected ConfigurableEnvironment getEnvironment() { - ConfigurableEnvironment environment = super.getEnvironment(); + ConfigurableEnvironment environment = new StandardEnvironment(); MutablePropertySources sources = environment.getPropertySources(); PropertySource source = sources.get(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME); Map map = new LinkedHashMap<>((Map) source.getSource());