Skip to content

Commit 7e6bad8

Browse files
committed
Fix @ActiveProfile to override existing profiles
Change SpringApplicationContextLoader to set active profiles using the `spring.profiles.active` environment property rather than calling `SpringApplication.setAdditionalProfiles`. This allows @activeprofiles to replace existing profiles rather than add to them which is consistent with the Spring TestContext Framework. Fixes gh-1469
1 parent c2ff7ca commit 7e6bad8

File tree

2 files changed

+62
-3
lines changed

2 files changed

+62
-3
lines changed

spring-boot/src/main/java/org/springframework/boot/test/SpringApplicationContextLoader.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import org.springframework.test.context.web.WebAppConfiguration;
4949
import org.springframework.test.context.web.WebMergedContextConfiguration;
5050
import org.springframework.util.ObjectUtils;
51+
import org.springframework.util.StringUtils;
5152
import org.springframework.web.context.support.GenericWebApplicationContext;
5253
import org.springframework.web.context.support.StandardServletEnvironment;
5354

@@ -80,13 +81,16 @@ public ApplicationContext loadContext(MergedContextConfiguration config)
8081
throws Exception {
8182
SpringApplication application = getSpringApplication();
8283
application.setSources(getSources(config));
83-
if (!ObjectUtils.isEmpty(config.getActiveProfiles())) {
84-
application.setAdditionalProfiles(config.getActiveProfiles());
85-
}
8684
ConfigurableEnvironment environment = new StandardEnvironment();
8785
if (config instanceof WebMergedContextConfiguration) {
8886
environment = new StandardServletEnvironment();
8987
}
88+
if (!ObjectUtils.isEmpty(config.getActiveProfiles())) {
89+
String profiles = StringUtils.arrayToCommaDelimitedString(config
90+
.getActiveProfiles());
91+
EnvironmentTestUtils.addEnvironment(environment, "spring.profiles.active="
92+
+ profiles);
93+
}
9094
// Ensure @IntegrationTest properties go before external config and after system
9195
environment.getPropertySources()
9296
.addAfter(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2012-2014 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+
* http://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;
18+
19+
import org.junit.Test;
20+
import org.junit.runner.RunWith;
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.context.ApplicationContext;
23+
import org.springframework.context.annotation.Configuration;
24+
import org.springframework.test.context.ActiveProfiles;
25+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
26+
27+
import static org.hamcrest.Matchers.equalTo;
28+
import static org.junit.Assert.assertThat;
29+
30+
/**
31+
* Tests for {@link SpringApplicationContextLoader} with active profiles. See gh-1469.
32+
*
33+
* @author Phillip Webb
34+
*/
35+
@RunWith(SpringJUnit4ClassRunner.class)
36+
@SpringApplicationConfiguration
37+
@IntegrationTest("spring.config.name=enableother")
38+
@ActiveProfiles("override")
39+
public class SpringApplicationConfigurationActiveProfileTests {
40+
41+
@Autowired
42+
private ApplicationContext context;
43+
44+
@Test
45+
public void profiles() throws Exception {
46+
assertThat(this.context.getEnvironment().getActiveProfiles(),
47+
equalTo(new String[] { "override" }));
48+
}
49+
50+
@Configuration
51+
protected static class Config {
52+
53+
}
54+
55+
}

0 commit comments

Comments
 (0)