From c84d3c14fb3bfd9e4eba97792566fc8f2ec5b322 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Fri, 14 Jan 2022 16:30:28 -0800 Subject: [PATCH] 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 --- .../test/context/SpringBootContextLoader.java | 21 ------------------- .../context/SpringBootContextLoaderTests.java | 2 +- 2 files changed, 1 insertion(+), 22 deletions(-) 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 95b27ca8ced..01694563c03 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 @@ -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> 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, diff --git a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringBootContextLoaderTests.java b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringBootContextLoaderTests.java index c0554f67992..ad03d67f4a0 100644 --- a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringBootContextLoaderTests.java +++ b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/SpringBootContextLoaderTests.java @@ -140,7 +140,7 @@ class SpringBootContextLoaderTests { List 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");