Merge branch '2.7.x'

Closes gh-29408
This commit is contained in:
Phillip Webb 2022-01-14 17:36:20 -08:00
commit 32e8b30c36
3 changed files with 11 additions and 13 deletions

View File

@ -42,7 +42,6 @@ import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.core.annotation.MergedAnnotations.SearchStrategy; import org.springframework.core.annotation.MergedAnnotations.SearchStrategy;
import org.springframework.core.annotation.Order; import org.springframework.core.annotation.Order;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader; import org.springframework.core.io.ResourceLoader;
import org.springframework.test.context.ContextConfigurationAttributes; 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.test.context.web.WebMergedContextConfiguration;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.web.context.support.GenericWebApplicationContext; import org.springframework.web.context.support.GenericWebApplicationContext;
@ -114,10 +112,8 @@ public class SpringBootContextLoader extends AbstractContextLoader {
application.setWebApplicationType(WebApplicationType.NONE); application.setWebApplicationType(WebApplicationType.NONE);
} }
application.setInitializers(initializers); application.setInitializers(initializers);
boolean customEnvironent = ReflectionUtils.findMethod(getClass(), "getEnvironment") ConfigurableEnvironment environment = getEnvironment();
.getDeclaringClass() != SpringBootContextLoader.class; if (environment != null) {
if (customEnvironent) {
ConfigurableEnvironment environment = getEnvironment();
prepareEnvironment(config, application, environment, false); prepareEnvironment(config, application, environment, false);
application.setEnvironment(environment); application.setEnvironment(environment);
} }
@ -163,12 +159,13 @@ public class SpringBootContextLoader extends AbstractContextLoader {
} }
/** /**
* Builds a new {@link ConfigurableEnvironment} instance. You can override this method * Returns the {@link ConfigurableEnvironment} instance that should be applied to
* to return something other than {@link StandardEnvironment} if necessary. * {@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 * @return a {@link ConfigurableEnvironment} instance
*/ */
protected ConfigurableEnvironment getEnvironment() { protected ConfigurableEnvironment getEnvironment() {
return new StandardEnvironment(); return null;
} }
protected String[] getInlinedProperties(MergedContextConfiguration config) { protected String[] getInlinedProperties(MergedContextConfiguration config) {

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.Environment;
import org.springframework.core.env.MapPropertySource; import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources; import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
@ -61,7 +62,7 @@ class SpringBootTestWithActiveProfilesAndEnvironmentPropertyTests {
@Override @Override
protected ConfigurableEnvironment getEnvironment() { protected ConfigurableEnvironment getEnvironment() {
ConfigurableEnvironment environment = super.getEnvironment(); ConfigurableEnvironment environment = new StandardEnvironment();
MutablePropertySources sources = environment.getPropertySources(); MutablePropertySources sources = environment.getPropertySources();
Map<String, Object> map = new LinkedHashMap<>(); Map<String, Object> map = new LinkedHashMap<>();
map.put("spring.profiles.active", "local"); map.put("spring.profiles.active", "local");

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -64,7 +64,7 @@ class SpringBootTestWithActiveProfilesAndSystemEnvironmentPropertyTests {
@Override @Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
protected ConfigurableEnvironment getEnvironment() { protected ConfigurableEnvironment getEnvironment() {
ConfigurableEnvironment environment = super.getEnvironment(); ConfigurableEnvironment environment = new StandardEnvironment();
MutablePropertySources sources = environment.getPropertySources(); MutablePropertySources sources = environment.getPropertySources();
PropertySource<?> source = sources.get(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME); PropertySource<?> source = sources.get(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME);
Map<String, Object> map = new LinkedHashMap<>((Map<String, Object>) source.getSource()); Map<String, Object> map = new LinkedHashMap<>((Map<String, Object>) source.getSource());