Skip to content

Commit 4301580

Browse files
committed
Improve testing of PropertyPlaceholderAutoConfiguration
Since Spring Framework 4.3.0.RC2, a default embedded value resolver has been registered with the bean factory when one is not otherwise configured. This meant that placeholders in `@Value` would be resolved with or without PropertyPlaceholderAutoConfiguration defining a PropertySourcesPlaceholderConfigurer bean. However, placeholders in bean definitions would only be resolved if a PropertySourcesPlaceholderConfigurer was defined. This commit updates PropertyPlaceholderAutoConfigurationTests to align with this change in Framework. We now test that placeholders are resolved in `@Value` annotations with or without the auto-configuration and that placeholders in bean definitions are only resolved with the auto-configured. Closes gh-22230
1 parent 21453b5 commit 4301580

File tree

1 file changed

+61
-26
lines changed

1 file changed

+61
-26
lines changed

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/context/PropertyPlaceholderAutoConfigurationTests.java

Lines changed: 61 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,12 +16,14 @@
1616

1717
package org.springframework.boot.autoconfigure.context;
1818

19-
import org.junit.jupiter.api.AfterEach;
2019
import org.junit.jupiter.api.Test;
2120

2221
import org.springframework.beans.factory.annotation.Value;
23-
import org.springframework.boot.test.util.TestPropertyValues;
24-
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
22+
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
23+
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
24+
import org.springframework.boot.autoconfigure.AutoConfigurations;
25+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
26+
import org.springframework.context.ConfigurableApplicationContext;
2527
import org.springframework.context.annotation.Bean;
2628
import org.springframework.context.annotation.Configuration;
2729
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
@@ -33,43 +35,75 @@
3335
* Tests for {@link PropertyPlaceholderAutoConfiguration}.
3436
*
3537
* @author Dave Syer
38+
* @author Andy Wilkinson
3639
*/
3740
class PropertyPlaceholderAutoConfigurationTests {
3841

39-
private final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
42+
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner();
4043

41-
@AfterEach
42-
void close() {
43-
if (this.context != null) {
44-
this.context.close();
45-
}
44+
@Test
45+
void whenTheAutoConfigurationIsNotUsedThenBeanDefinitionPlaceholdersAreNotResolved() {
46+
this.contextRunner.withPropertyValues("fruit:banana").withInitializer(this::definePlaceholderBean)
47+
.run((context) -> assertThat(context.getBean(PlaceholderBean.class).fruit).isEqualTo("${fruit:apple}"));
48+
}
49+
50+
@Test
51+
void whenTheAutoConfigurationIsUsedThenBeanDefinitionPlaceholdersAreResolved() {
52+
this.contextRunner.withPropertyValues("fruit:banana").withInitializer(this::definePlaceholderBean)
53+
.withConfiguration(AutoConfigurations.of(PropertyPlaceholderAutoConfiguration.class))
54+
.run((context) -> assertThat(context.getBean(PlaceholderBean.class).fruit).isEqualTo("banana"));
4655
}
4756

4857
@Test
49-
void propertyPlaceholders() {
50-
this.context.register(PropertyPlaceholderAutoConfiguration.class, PlaceholderConfig.class);
51-
TestPropertyValues.of("foo:two").applyTo(this.context);
52-
this.context.refresh();
53-
assertThat(this.context.getBean(PlaceholderConfig.class).getFoo()).isEqualTo("two");
58+
void whenTheAutoConfigurationIsNotUsedThenValuePlaceholdersAreResolved() {
59+
this.contextRunner.withPropertyValues("fruit:banana").withUserConfiguration(PlaceholderConfig.class)
60+
.run((context) -> assertThat(context.getBean(PlaceholderConfig.class).fruit).isEqualTo("banana"));
5461
}
5562

5663
@Test
57-
void propertyPlaceholdersOverride() {
58-
this.context.register(PropertyPlaceholderAutoConfiguration.class, PlaceholderConfig.class,
59-
PlaceholdersOverride.class);
60-
TestPropertyValues.of("foo:two").applyTo(this.context);
61-
this.context.refresh();
62-
assertThat(this.context.getBean(PlaceholderConfig.class).getFoo()).isEqualTo("spam");
64+
void whenTheAutoConfigurationIsUsedThenValuePlaceholdersAreResolved() {
65+
this.contextRunner.withPropertyValues("fruit:banana")
66+
.withConfiguration(AutoConfigurations.of(PropertyPlaceholderAutoConfiguration.class))
67+
.withUserConfiguration(PlaceholderConfig.class)
68+
.run((context) -> assertThat(context.getBean(PlaceholderConfig.class).fruit).isEqualTo("banana"));
69+
}
70+
71+
@Test
72+
void whenThereIsAUserDefinedPropertySourcesPlaceholderConfigurerThenItIsUsedForBeanDefinitionPlaceholderResolution() {
73+
this.contextRunner.withPropertyValues("fruit:banana").withInitializer(this::definePlaceholderBean)
74+
.withConfiguration(AutoConfigurations.of(PropertyPlaceholderAutoConfiguration.class))
75+
.withUserConfiguration(PlaceholdersOverride.class)
76+
.run((context) -> assertThat(context.getBean(PlaceholderBean.class).fruit).isEqualTo("orange"));
77+
}
78+
79+
@Test
80+
void whenThereIsAUserDefinedPropertySourcesPlaceholderConfigurerThenItIsUsedForValuePlaceholderResolution() {
81+
this.contextRunner.withPropertyValues("fruit:banana")
82+
.withConfiguration(AutoConfigurations.of(PropertyPlaceholderAutoConfiguration.class))
83+
.withUserConfiguration(PlaceholderConfig.class, PlaceholdersOverride.class)
84+
.run((context) -> assertThat(context.getBean(PlaceholderConfig.class).fruit).isEqualTo("orange"));
85+
}
86+
87+
private void definePlaceholderBean(ConfigurableApplicationContext context) {
88+
((BeanDefinitionRegistry) context.getBeanFactory()).registerBeanDefinition("placeholderBean",
89+
BeanDefinitionBuilder.genericBeanDefinition(PlaceholderBean.class)
90+
.addConstructorArgValue("${fruit:apple}").getBeanDefinition());
6391
}
6492

6593
@Configuration(proxyBeanMethods = false)
6694
static class PlaceholderConfig {
6795

68-
@Value("${foo:bar}")
69-
private String foo;
96+
@Value("${fruit:apple}")
97+
private String fruit;
98+
99+
}
100+
101+
static class PlaceholderBean {
102+
103+
private final String fruit;
70104

71-
String getFoo() {
72-
return this.foo;
105+
PlaceholderBean(String fruit) {
106+
this.fruit = fruit;
73107
}
74108

75109
}
@@ -80,7 +114,8 @@ static class PlaceholdersOverride {
80114
@Bean
81115
static PropertySourcesPlaceholderConfigurer morePlaceholders() {
82116
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
83-
configurer.setProperties(StringUtils.splitArrayElementsIntoProperties(new String[] { "foo=spam" }, "="));
117+
configurer
118+
.setProperties(StringUtils.splitArrayElementsIntoProperties(new String[] { "fruit=orange" }, "="));
84119
configurer.setLocalOverride(true);
85120
configurer.setOrder(0);
86121
return configurer;

0 commit comments

Comments
 (0)