Document precedence for @DynamicPropertySource
Closes gh-24837
This commit is contained in:
@@ -16,38 +16,71 @@
|
||||
|
||||
package org.springframework.test.context;
|
||||
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestInstance;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.core.env.MutablePropertySources;
|
||||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS;
|
||||
|
||||
/**
|
||||
* Integration test for {@link DynamicPropertySource @DynamicPropertySource}.
|
||||
* Integration tests for {@link DynamicPropertySource @DynamicPropertySource}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Sam Brannen
|
||||
*/
|
||||
@SpringJUnitConfig
|
||||
@TestPropertySource(properties = "test.container.ip: test")
|
||||
@TestInstance(PER_CLASS)
|
||||
@DisplayName("@DynamicPropertySource integration tests")
|
||||
class DynamicPropertySourceIntegrationTests {
|
||||
|
||||
private static final String TEST_CONTAINER_IP = "test.container.ip";
|
||||
|
||||
static {
|
||||
System.setProperty(TEST_CONTAINER_IP, "system");
|
||||
}
|
||||
|
||||
static DemoContainer container = new DemoContainer();
|
||||
|
||||
|
||||
@DynamicPropertySource
|
||||
static void containerProperties(DynamicPropertyRegistry registry) {
|
||||
registry.add("test.container.ip", container::getIpAddress);
|
||||
registry.add(TEST_CONTAINER_IP, container::getIpAddress);
|
||||
registry.add("test.container.port", container::getPort);
|
||||
}
|
||||
|
||||
|
||||
@AfterAll
|
||||
void clearSystemProperty() {
|
||||
System.clearProperty(TEST_CONTAINER_IP);
|
||||
}
|
||||
|
||||
@Test
|
||||
void hasInjectedValues(@Autowired Service service) {
|
||||
@DisplayName("@DynamicPropertySource overrides @TestPropertySource and JVM system property")
|
||||
void dynamicPropertySourceOverridesTestPropertySourceAndSystemProperty(@Autowired ConfigurableEnvironment env) {
|
||||
MutablePropertySources propertySources = env.getPropertySources();
|
||||
assertThat(propertySources.size()).isGreaterThanOrEqualTo(4);
|
||||
assertThat(propertySources.contains("Dynamic Test Properties")).isTrue();
|
||||
assertThat(propertySources.contains("Inlined Test Properties")).isTrue();
|
||||
assertThat(propertySources.contains("systemProperties")).isTrue();
|
||||
assertThat(propertySources.get("Dynamic Test Properties").getProperty(TEST_CONTAINER_IP)).isEqualTo("127.0.0.1");
|
||||
assertThat(propertySources.get("Inlined Test Properties").getProperty(TEST_CONTAINER_IP)).isEqualTo("test");
|
||||
assertThat(propertySources.get("systemProperties").getProperty(TEST_CONTAINER_IP)).isEqualTo("system");
|
||||
assertThat(env.getProperty(TEST_CONTAINER_IP)).isEqualTo("127.0.0.1");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("@Service has values injected from @DynamicPropertySource")
|
||||
void serviceHasInjectedValues(@Autowired Service service) {
|
||||
assertThat(service.getIp()).isEqualTo("127.0.0.1");
|
||||
assertThat(service.getPort()).isEqualTo(4242);
|
||||
}
|
||||
@@ -58,7 +91,6 @@ class DynamicPropertySourceIntegrationTests {
|
||||
static class Config {
|
||||
}
|
||||
|
||||
@Component
|
||||
static class Service {
|
||||
|
||||
private final String ip;
|
||||
|
||||
Reference in New Issue
Block a user