Change getEnvironment() to return null by default
Update the `SpringBootContextLoader.getEnvironment()` method so that it returns `null` rather than a new `StandardEnvironment`. Closes gh-29405
This commit is contained in:
parent
c84d3c14fb
commit
9764b3eba0
|
@ -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();
|
||||
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) {
|
||||
|
|
|
@ -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<String, Object> map = new LinkedHashMap<>();
|
||||
map.put("spring.profiles.active", "local");
|
||||
|
|
|
@ -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<String, Object> map = new LinkedHashMap<>((Map<String, Object>) source.getSource());
|
||||
|
|
Loading…
Reference in New Issue