Allow test property sources to override command line args

Refine `SpringBootContextLoader` logic so that inline test properties
are always added above command line arguments.

Closes gh-29404
This commit is contained in:
Phillip Webb 2022-01-14 16:30:28 -08:00
parent 9278a502c7
commit c84d3c14fb
2 changed files with 1 additions and 22 deletions

View File

@ -18,12 +18,10 @@ package org.springframework.boot.test.context;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.boot.ApplicationContextFactory;
import org.springframework.boot.DefaultPropertiesPropertySource;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
@ -43,10 +41,7 @@ import org.springframework.core.SpringVersion;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.core.annotation.MergedAnnotations.SearchStrategy;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.CommandLinePropertySource;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader;
@ -89,9 +84,6 @@ import org.springframework.web.context.support.GenericWebApplicationContext;
*/
public class SpringBootContextLoader extends AbstractContextLoader {
private static final String[] PRIORITY_PROPERTY_SOURCES = { "configurationProperties",
DefaultPropertiesPropertySource.NAME, CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME };
@Override
public ApplicationContext loadContext(MergedContextConfiguration config) throws Exception {
Class<?>[] configClasses = config.getClasses();
@ -138,25 +130,12 @@ public class SpringBootContextLoader extends AbstractContextLoader {
private void prepareEnvironment(MergedContextConfiguration config, SpringApplication application,
ConfigurableEnvironment environment, boolean applicationEnvironment) {
MutablePropertySources propertySources = environment.getPropertySources();
List<PropertySource<?>> priorityPropertySources = new ArrayList<>();
if (applicationEnvironment) {
for (String priorityPropertySourceName : PRIORITY_PROPERTY_SOURCES) {
PropertySource<?> priorityPropertySource = propertySources.get(priorityPropertySourceName);
if (priorityPropertySource != null) {
priorityPropertySources.add(priorityPropertySource);
propertySources.remove(priorityPropertySourceName);
}
}
}
setActiveProfiles(environment, config.getActiveProfiles(), applicationEnvironment);
ResourceLoader resourceLoader = (application.getResourceLoader() != null) ? application.getResourceLoader()
: new DefaultResourceLoader(null);
TestPropertySourceUtils.addPropertiesFilesToEnvironment(environment, resourceLoader,
config.getPropertySourceLocations());
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(environment, getInlinedProperties(config));
Collections.reverse(priorityPropertySources);
priorityPropertySources.forEach(propertySources::addFirst);
}
private void setActiveProfiles(ConfigurableEnvironment environment, String[] profiles,

View File

@ -140,7 +140,7 @@ class SpringBootContextLoaderTests {
List<String> names = environment.getPropertySources().stream().map(PropertySource::getName)
.collect(Collectors.toList());
String last = names.remove(names.size() - 1);
assertThat(names).containsExactly("configurationProperties", "commandLineArgs", "Inlined Test Properties",
assertThat(names).containsExactly("configurationProperties", "Inlined Test Properties", "commandLineArgs",
"servletConfigInitParams", "servletContextInitParams", "systemProperties", "systemEnvironment",
"random");
assertThat(last).startsWith("Config resource");