Skip to content

Commit 49921d6

Browse files
committed
Ensure @activeprofiles replaces existing profiles
Update `SpringBootContextLoader` to both add `spring.profiles.active` properties and to directly call `Environment.setActiveProfiles`. The additional `setActiveProfiles` call prevents `AbstractEnvironment` from accidentally loading `spring.profiles.active` properties directly when `doGetActiveProfiles` is called. Directly setting active profiles has only become necessary since we started adding properties using the square bracket notation. Previously we added a comma-separated list which would be picked up by both the `AbstractEnvironment` and the `ConfigurationFileApplicationListener`. Closes gh-21302
1 parent 28749e7 commit 49921d6

File tree

3 files changed

+155
-0
lines changed

3 files changed

+155
-0
lines changed

spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootContextLoader.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ protected String[] getArgs(MergedContextConfiguration config) {
152152
}
153153

154154
private void setActiveProfiles(ConfigurableEnvironment environment, String[] profiles) {
155+
environment.setActiveProfiles(profiles);
156+
// Also add as properties to override any application.properties
155157
String[] pairs = new String[profiles.length];
156158
for (int i = 0; i < profiles.length; i++) {
157159
pairs[i] = "spring.profiles.active[" + i + "]=" + profiles[i];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2012-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test.context;
18+
19+
import java.util.LinkedHashMap;
20+
import java.util.Map;
21+
22+
import org.junit.jupiter.api.Test;
23+
24+
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.context.annotation.Configuration;
26+
import org.springframework.core.env.ConfigurableEnvironment;
27+
import org.springframework.core.env.Environment;
28+
import org.springframework.core.env.MapPropertySource;
29+
import org.springframework.core.env.MutablePropertySources;
30+
import org.springframework.test.context.ActiveProfiles;
31+
import org.springframework.test.context.ContextConfiguration;
32+
33+
import static org.assertj.core.api.Assertions.assertThat;
34+
35+
/**
36+
* Integration tests for {@link SpringBootTest @SpringBootTest} with an
37+
* {@link ActiveProfiles @ActiveProfiles} annotation.
38+
*
39+
* @author Johnny Lim
40+
* @author Phillip Webb
41+
*/
42+
@SpringBootTest
43+
@ActiveProfiles({ "test1", "test2" })
44+
@ContextConfiguration(loader = SpringBootTestWithActiveProfilesAndEnvironmentPropertyTests.Loader.class)
45+
public class SpringBootTestWithActiveProfilesAndEnvironmentPropertyTests {
46+
47+
@Autowired
48+
private Environment environment;
49+
50+
@Test
51+
void getActiveProfiles() {
52+
assertThat(this.environment.getActiveProfiles()).containsOnly("test1", "test2");
53+
}
54+
55+
@Configuration
56+
static class Config {
57+
58+
}
59+
60+
static class Loader extends SpringBootContextLoader {
61+
62+
@Override
63+
protected ConfigurableEnvironment getEnvironment() {
64+
ConfigurableEnvironment environment = super.getEnvironment();
65+
MutablePropertySources sources = environment.getPropertySources();
66+
Map<String, Object> map = new LinkedHashMap<>();
67+
map.put("spring.profiles.active", "local");
68+
sources.addLast(new MapPropertySource("profiletest", map));
69+
return environment;
70+
}
71+
72+
}
73+
74+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2012-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test.context;
18+
19+
import java.util.LinkedHashMap;
20+
import java.util.Map;
21+
22+
import org.junit.jupiter.api.Test;
23+
24+
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.context.annotation.Configuration;
26+
import org.springframework.core.env.ConfigurableEnvironment;
27+
import org.springframework.core.env.Environment;
28+
import org.springframework.core.env.MapPropertySource;
29+
import org.springframework.core.env.MutablePropertySources;
30+
import org.springframework.core.env.PropertySource;
31+
import org.springframework.core.env.StandardEnvironment;
32+
import org.springframework.test.context.ActiveProfiles;
33+
import org.springframework.test.context.ContextConfiguration;
34+
35+
import static org.assertj.core.api.Assertions.assertThat;
36+
37+
/**
38+
* Integration tests for {@link SpringBootTest @SpringBootTest} with an
39+
* {@link ActiveProfiles @ActiveProfiles} annotation.
40+
*
41+
* @author Johnny Lim
42+
* @author Phillip Webb
43+
*/
44+
@SpringBootTest
45+
@ActiveProfiles({ "test1", "test2" })
46+
@ContextConfiguration(loader = SpringBootTestWithActiveProfilesAndSytemEnvironmentPropertyTests.Loader.class)
47+
public class SpringBootTestWithActiveProfilesAndSytemEnvironmentPropertyTests {
48+
49+
@Autowired
50+
private Environment environment;
51+
52+
@Test
53+
void getActiveProfiles() {
54+
assertThat(this.environment.getActiveProfiles()).containsOnly("test1", "test2");
55+
}
56+
57+
@Configuration
58+
static class Config {
59+
60+
}
61+
62+
static class Loader extends SpringBootContextLoader {
63+
64+
@Override
65+
@SuppressWarnings("unchecked")
66+
protected ConfigurableEnvironment getEnvironment() {
67+
ConfigurableEnvironment environment = super.getEnvironment();
68+
MutablePropertySources sources = environment.getPropertySources();
69+
PropertySource<?> source = sources.get(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME);
70+
Map<String, Object> map = new LinkedHashMap<>((Map<String, Object>) source.getSource());
71+
map.put("SPRING_PROFILES_ACTIVE", "local");
72+
sources.replace(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME,
73+
new MapPropertySource(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, map));
74+
return environment;
75+
}
76+
77+
}
78+
79+
}

0 commit comments

Comments
 (0)