|
| 1 | +/* |
| 2 | + * Copyright 2002-2020 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.test.context.junit.jupiter.nested; |
| 18 | + |
| 19 | +import org.junit.jupiter.api.DisplayName; |
| 20 | +import org.junit.jupiter.api.Nested; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | + |
| 23 | +import org.springframework.beans.factory.annotation.Autowired; |
| 24 | +import org.springframework.beans.factory.annotation.Value; |
| 25 | +import org.springframework.context.annotation.Configuration; |
| 26 | +import org.springframework.context.annotation.Import; |
| 27 | +import org.springframework.test.context.DynamicPropertyRegistry; |
| 28 | +import org.springframework.test.context.DynamicPropertySource; |
| 29 | +import org.springframework.test.context.NestedTestConfiguration; |
| 30 | +import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 31 | +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; |
| 32 | + |
| 33 | +import static org.assertj.core.api.Assertions.assertThat; |
| 34 | +import static org.springframework.test.context.NestedTestConfiguration.EnclosingConfiguration.OVERRIDE; |
| 35 | + |
| 36 | +/** |
| 37 | + * Integration tests that verify support for {@code @Nested} test classes using |
| 38 | + * {@link DynamicPropertySource @DynamicPropertySource} in conjunction with the |
| 39 | + * {@link SpringExtension} in a JUnit Jupiter environment. |
| 40 | + * |
| 41 | + * @author Sam Brannen |
| 42 | + * @since 5.3.2 |
| 43 | + */ |
| 44 | +@SpringJUnitConfig |
| 45 | +class DynamicPropertySourceNestedTests { |
| 46 | + |
| 47 | + private static final String TEST_CONTAINER_IP = "DynamicPropertySourceNestedTests.test.container.ip"; |
| 48 | + |
| 49 | + private static final String TEST_CONTAINER_PORT = "DynamicPropertySourceNestedTests.test.container.port"; |
| 50 | + |
| 51 | + static DemoContainer container = new DemoContainer(); |
| 52 | + |
| 53 | + @DynamicPropertySource |
| 54 | + static void containerProperties(DynamicPropertyRegistry registry) { |
| 55 | + registry.add(TEST_CONTAINER_IP, container::getIpAddress); |
| 56 | + registry.add(TEST_CONTAINER_PORT, container::getPort); |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | + @Test |
| 61 | + @DisplayName("@Service has values injected from @DynamicPropertySource") |
| 62 | + void serviceHasInjectedValues(@Autowired Service service) { |
| 63 | + assertServiceHasInjectedValues(service); |
| 64 | + } |
| 65 | + |
| 66 | + private static void assertServiceHasInjectedValues(Service service) { |
| 67 | + assertThat(service.getIp()).isEqualTo("127.0.0.1"); |
| 68 | + assertThat(service.getPort()).isEqualTo(4242); |
| 69 | + } |
| 70 | + |
| 71 | + @Nested |
| 72 | + @NestedTestConfiguration(OVERRIDE) |
| 73 | + @SpringJUnitConfig(Config.class) |
| 74 | + class DynamicPropertySourceFromSuperclassTests extends DynamicPropertySourceSuperclass { |
| 75 | + |
| 76 | + @Test |
| 77 | + @DisplayName("@Service has values injected from @DynamicPropertySource in superclass") |
| 78 | + void serviceHasInjectedValues(@Autowired Service service) { |
| 79 | + assertServiceHasInjectedValues(service); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @Nested |
| 84 | + @NestedTestConfiguration(OVERRIDE) |
| 85 | + @SpringJUnitConfig(Config.class) |
| 86 | + class DynamicPropertySourceFromInterfaceTests implements DynamicPropertySourceInterface { |
| 87 | + |
| 88 | + @Test |
| 89 | + @DisplayName("@Service has values injected from @DynamicPropertySource in interface") |
| 90 | + void serviceHasInjectedValues(@Autowired Service service) { |
| 91 | + assertServiceHasInjectedValues(service); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + @Nested |
| 96 | + @NestedTestConfiguration(OVERRIDE) |
| 97 | + @SpringJUnitConfig(Config.class) |
| 98 | + class OverriddenConfigTests { |
| 99 | + |
| 100 | + @Test |
| 101 | + @DisplayName("@Service does not have values injected from @DynamicPropertySource in enclosing class") |
| 102 | + void serviceHasDefaultInjectedValues(@Autowired Service service) { |
| 103 | + assertThat(service.getIp()).isEqualTo("10.0.0.1"); |
| 104 | + assertThat(service.getPort()).isEqualTo(-999); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + @Nested |
| 109 | + class DynamicPropertySourceFromEnclosingClassTests { |
| 110 | + |
| 111 | + @Test |
| 112 | + @DisplayName("@Service has values injected from @DynamicPropertySource in enclosing class") |
| 113 | + void serviceHasInjectedValues(@Autowired Service service) { |
| 114 | + assertServiceHasInjectedValues(service); |
| 115 | + } |
| 116 | + |
| 117 | + @Nested |
| 118 | + class DoubleNestedDynamicPropertySourceFromEnclosingClassTests { |
| 119 | + |
| 120 | + @Test |
| 121 | + @DisplayName("@Service has values injected from @DynamicPropertySource in enclosing class") |
| 122 | + void serviceHasInjectedValues(@Autowired Service service) { |
| 123 | + assertServiceHasInjectedValues(service); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + |
| 129 | + static abstract class DynamicPropertySourceSuperclass { |
| 130 | + |
| 131 | + @DynamicPropertySource |
| 132 | + static void containerProperties(DynamicPropertyRegistry registry) { |
| 133 | + registry.add(TEST_CONTAINER_IP, container::getIpAddress); |
| 134 | + registry.add(TEST_CONTAINER_PORT, container::getPort); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + interface DynamicPropertySourceInterface { |
| 139 | + |
| 140 | + @DynamicPropertySource |
| 141 | + static void containerProperties(DynamicPropertyRegistry registry) { |
| 142 | + registry.add(TEST_CONTAINER_IP, container::getIpAddress); |
| 143 | + registry.add(TEST_CONTAINER_PORT, container::getPort); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + |
| 148 | + @Configuration |
| 149 | + @Import(Service.class) |
| 150 | + static class Config { |
| 151 | + } |
| 152 | + |
| 153 | + static class Service { |
| 154 | + |
| 155 | + private final String ip; |
| 156 | + |
| 157 | + private final int port; |
| 158 | + |
| 159 | + |
| 160 | + Service(@Value("${" + TEST_CONTAINER_IP + ":10.0.0.1}") String ip, @Value("${" + TEST_CONTAINER_PORT + ":-999}") int port) { |
| 161 | + this.ip = ip; |
| 162 | + this.port = port; |
| 163 | + } |
| 164 | + |
| 165 | + String getIp() { |
| 166 | + return this.ip; |
| 167 | + } |
| 168 | + |
| 169 | + int getPort() { |
| 170 | + return this.port; |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + static class DemoContainer { |
| 175 | + |
| 176 | + String getIpAddress() { |
| 177 | + return "127.0.0.1"; |
| 178 | + } |
| 179 | + |
| 180 | + int getPort() { |
| 181 | + return 4242; |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | +} |
0 commit comments