|
| 1 | +/* |
| 2 | + * Copyright 2002-2024 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 | + * https://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.experimental.boot.test.context; |
| 18 | + |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +import org.springframework.beans.factory.annotation.Autowired; |
| 22 | +import org.springframework.beans.factory.annotation.Value; |
| 23 | +import org.springframework.context.annotation.Bean; |
| 24 | +import org.springframework.context.annotation.Configuration; |
| 25 | +import org.springframework.context.annotation.Import; |
| 26 | +import org.springframework.core.env.ConfigurableEnvironment; |
| 27 | +import org.springframework.core.env.Environment; |
| 28 | +import org.springframework.test.context.DynamicPropertyRegistry; |
| 29 | +import org.springframework.test.context.TestPropertySource; |
| 30 | +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; |
| 31 | + |
| 32 | +import static org.assertj.core.api.Assertions.assertThat; |
| 33 | + |
| 34 | +/** |
| 35 | + * Integration tests for {@link DynamicPropertyRegistry} bean support. |
| 36 | + * |
| 37 | + * @author Sam Brannen |
| 38 | + * @since 6.2 |
| 39 | + */ |
| 40 | +@SpringJUnitConfig |
| 41 | +@TestPropertySource(properties = "api.url: https://example.com/test") |
| 42 | +class DynamicPropertyRegistryIntegrationTests { |
| 43 | + |
| 44 | + private static final String API_URL = "api.url"; |
| 45 | + |
| 46 | + @Test |
| 47 | + void dynamicPropertySourceOverridesTestPropertySource(@Autowired ConfigurableEnvironment env) { |
| 48 | + assertThat(env.getProperty(API_URL)).isEqualTo("https://example.com/dynamic"); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + void environmentInjectedServiceCanRetrieveDynamicProperty(@Autowired EnvironmentInjectedService service) { |
| 53 | + assertThat(service.getApiUrl()).isEqualTo("https://example.com/dynamic"); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void dependentServiceReceivesDynamicProperty(@Autowired DependentService service) { |
| 58 | + assertThat(service.getApiUrl()).isEqualTo("https://example.com/dynamic"); |
| 59 | + } |
| 60 | + |
| 61 | + @Configuration |
| 62 | + @Import({ EnvironmentInjectedService.class, ValueInjectedService.class }) |
| 63 | + @EnableDynamicProperty |
| 64 | + static class Config { |
| 65 | + |
| 66 | + @Bean |
| 67 | + DependentService dependentService() { |
| 68 | + return new DependentService(); |
| 69 | + } |
| 70 | + |
| 71 | + @Bean |
| 72 | + @DynamicProperty(name = "api.url", value = "url") |
| 73 | + ApiServer apiServer() { |
| 74 | + ApiServer apiServer = new ApiServer(); |
| 75 | + return apiServer; |
| 76 | + } |
| 77 | + |
| 78 | + } |
| 79 | + |
| 80 | + static class EnvironmentInjectedService { |
| 81 | + |
| 82 | + private final Environment env; |
| 83 | + |
| 84 | + EnvironmentInjectedService(Environment env) { |
| 85 | + this.env = env; |
| 86 | + } |
| 87 | + |
| 88 | + String getApiUrl() { |
| 89 | + return this.env.getProperty(API_URL); |
| 90 | + } |
| 91 | + |
| 92 | + } |
| 93 | + |
| 94 | + static class DependentService { |
| 95 | + |
| 96 | + private String apiUrl; |
| 97 | + |
| 98 | + @Autowired |
| 99 | + void setApiUrl(@Value("${api.url}") String apiUrl) { |
| 100 | + this.apiUrl = apiUrl; |
| 101 | + } |
| 102 | + |
| 103 | + String getApiUrl() { |
| 104 | + return this.apiUrl; |
| 105 | + } |
| 106 | + |
| 107 | + } |
| 108 | + |
| 109 | + static class ValueInjectedService { |
| 110 | + |
| 111 | + private final String apiUrl; |
| 112 | + |
| 113 | + ValueInjectedService(@Value("${api.url}") String apiUrl) { |
| 114 | + this.apiUrl = apiUrl; |
| 115 | + } |
| 116 | + |
| 117 | + String getApiUrl() { |
| 118 | + return this.apiUrl; |
| 119 | + } |
| 120 | + |
| 121 | + } |
| 122 | + |
| 123 | + public static class ApiServer { |
| 124 | + |
| 125 | + public String getUrl() { |
| 126 | + return "https://example.com/dynamic"; |
| 127 | + } |
| 128 | + |
| 129 | + } |
| 130 | + |
| 131 | +} |
0 commit comments