-
Notifications
You must be signed in to change notification settings - Fork 41.5k
Description
Spring-Boot version: spring-boot-2.0.1.RELEASE
Use case:
I have the following application.yaml
server:
address: 192.168.1.100
---
spring:
profiles: production | eu-central
server:
address: 192.168.1.120
---
spring:
profiles: development
server:
address: 127.0.0.1
Also I have TestProfile class
@ActiveProfiles(value = "production")
@RunWith(SpringRunner.class)
@SpringBootTest
public class ProfileTest {
@Autowired
private Environment environment;
@Value("${server.address}")
private String serverAddress;
@Test
public void testProfile() {
Assertions.assertThat(environment.getActiveProfiles()).isEqualTo(new String[] {"production"} );
Assertions.assertThat(serverAddress).isEqualTo("192.168.1.120");
}
}
And I've got an error
org.junit.ComparisonFailure: Expected :"192.168.1.120" Actual :"192.168.1.100"
The problem is SpringBoot always use default value for ${server.address} because framework couldn't recognise profiles: production | eu-central as a List of profiles. If I use
@ActiveProfiles(value = "production | eu-central")
then my test is ok.
I've found that problem comes from org.springframework.boot.context.config.ConfigFileApplicationListener.java
private List<Document> asDocuments(List<PropertySource<?>> loaded) {
if (loaded == null) {
return Collections.emptyList();
}
return loaded.stream().map((propertySource) -> {
Binder binder = new Binder(
ConfigurationPropertySources.from(propertySource),
new PropertySourcesPlaceholdersResolver(this.environment));
return new Document(propertySource,
binder.bind("spring.profiles", Bindable.of(String[].class))
.orElse(null),
getProfiles(binder, ACTIVE_PROFILES_PROPERTY),
getProfiles(binder, INCLUDE_PROFILES_PROPERTY));
}).collect(Collectors.toList());
}
the following line
binder.bind("spring.profiles", Bindable.of(String[].class)).orElse(null)
construct Document object and this object always contain profiles array with one element. In my case it contain Array with one String "production | eu-central". I think that is root of cause.