From fae7a09b83e3e2d4abbd7a9454a189c0a6da4cd7 Mon Sep 17 00:00:00 2001 From: Nguyen Duy Tiep Date: Sat, 21 Oct 2017 21:58:04 +0800 Subject: [PATCH] Support custom SpringBootContextLoader environment Provide a `getEnvironment` method in `SpringBootContextLoader` to allow specialized `ConfigurableEnvironment` implementations to be used. Closes gh-10740 --- .../boot/test/context/SpringBootContextLoader.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) 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 267bcb90e8..69b1ec9d27 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 @@ -105,7 +105,7 @@ public class SpringBootContextLoader extends AbstractContextLoader { application.setMainApplicationClass(config.getTestClass()); application.addPrimarySources(Arrays.asList(configClasses)); application.getSources().addAll(Arrays.asList(configLocations)); - ConfigurableEnvironment environment = new StandardEnvironment(); + ConfigurableEnvironment environment = getEnvironment(); if (!ObjectUtils.isEmpty(config.getActiveProfiles())) { setActiveProfiles(environment, config.getActiveProfiles()); } @@ -148,6 +148,15 @@ public class SpringBootContextLoader extends AbstractContextLoader { return new SpringApplication(); } + /** + * Builds a new {@link ConfigurableEnvironment} instance. You can override this method + * to return something other than {@link StandardEnvironment} if necessary. + * @return a {@link ConfigurableEnvironment} instance + */ + protected ConfigurableEnvironment getEnvironment() { + return new StandardEnvironment(); + } + private void setActiveProfiles(ConfigurableEnvironment environment, String[] profiles) { TestPropertyValues