Perform placeholder resolution in test env post processor
Fixes gh-15354
This commit is contained in:
@@ -28,6 +28,7 @@ import org.springframework.mock.env.MockEnvironment;
|
||||
import org.springframework.test.context.support.TestPropertySourceUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Tests for {@link SpringBootTestRandomPortEnvironmentPostProcessor}.
|
||||
@@ -141,6 +142,56 @@ public class SpringBootTestRandomPortEnvironmentPostProcessorTests {
|
||||
assertThat(this.environment.getProperty("management.server.port")).isEqualTo("0");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void postProcessWhenManagementServerPortPlaceholderPresentShouldResolvePlaceholder() {
|
||||
addTestPropertySource("0", null);
|
||||
MapPropertySource testPropertySource = (MapPropertySource) this.propertySources
|
||||
.get(TestPropertySourceUtils.INLINED_PROPERTIES_PROPERTY_SOURCE_NAME);
|
||||
testPropertySource.getSource().put("port", "9090");
|
||||
this.propertySources.addLast(new MapPropertySource("other",
|
||||
Collections.singletonMap("management.server.port", "${port}")));
|
||||
this.postProcessor.postProcessEnvironment(this.environment, null);
|
||||
assertThat(this.environment.getProperty("server.port")).isEqualTo("0");
|
||||
assertThat(this.environment.getProperty("management.server.port")).isEqualTo("0");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void postProcessWhenManagementServerPortPlaceholderAbsentShouldFail() {
|
||||
addTestPropertySource("0", null);
|
||||
this.propertySources.addLast(new MapPropertySource("other",
|
||||
Collections.singletonMap("management.server.port", "${port}")));
|
||||
assertThatIllegalArgumentException().isThrownBy(
|
||||
() -> this.postProcessor.postProcessEnvironment(this.environment, null))
|
||||
.withMessage("Could not resolve placeholder 'port' in value \"${port}\"");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void postProcessWhenServerPortPlaceholderPresentShouldResolvePlaceholder() {
|
||||
addTestPropertySource("0", null);
|
||||
MapPropertySource testPropertySource = (MapPropertySource) this.propertySources
|
||||
.get(TestPropertySourceUtils.INLINED_PROPERTIES_PROPERTY_SOURCE_NAME);
|
||||
testPropertySource.getSource().put("port", "8080");
|
||||
Map<String, Object> source = new HashMap<>();
|
||||
source.put("server.port", "${port}");
|
||||
source.put("management.server.port", "9090");
|
||||
this.propertySources.addLast(new MapPropertySource("other", source));
|
||||
this.postProcessor.postProcessEnvironment(this.environment, null);
|
||||
assertThat(this.environment.getProperty("server.port")).isEqualTo("0");
|
||||
assertThat(this.environment.getProperty("management.server.port")).isEqualTo("0");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void postProcessWhenServerPortPlaceholderAbsentShouldFail() {
|
||||
addTestPropertySource("0", null);
|
||||
Map<String, Object> source = new HashMap<>();
|
||||
source.put("server.port", "${port}");
|
||||
source.put("management.server.port", "9090");
|
||||
this.propertySources.addLast(new MapPropertySource("other", source));
|
||||
assertThatIllegalArgumentException().isThrownBy(
|
||||
() -> this.postProcessor.postProcessEnvironment(this.environment, null))
|
||||
.withMessage("Could not resolve placeholder 'port' in value \"${port}\"");
|
||||
}
|
||||
|
||||
private void addTestPropertySource(String serverPort, String managementPort) {
|
||||
Map<String, Object> source = new HashMap<>();
|
||||
source.put("server.port", serverPort);
|
||||
|
||||
Reference in New Issue
Block a user