|
| 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 | +package org.springframework.test.context |
| 17 | + |
| 18 | +import org.assertj.core.api.Assertions |
| 19 | +import org.junit.jupiter.api.Test |
| 20 | +import org.springframework.beans.factory.annotation.Autowired |
| 21 | +import org.springframework.beans.factory.annotation.Value |
| 22 | +import org.springframework.context.annotation.Configuration |
| 23 | +import org.springframework.context.annotation.Import |
| 24 | +import org.springframework.stereotype.Component |
| 25 | +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig |
| 26 | + |
| 27 | +/** |
| 28 | + * Kotlin integration test for [@DynamicPropertySource][DynamicPropertySource]. |
| 29 | + * |
| 30 | + * @author Sebastien Deleuze |
| 31 | + * @author Phillip Webb |
| 32 | + * @author Sam Brannen |
| 33 | + */ |
| 34 | +@SpringJUnitConfig |
| 35 | +class KotlinDynamicPropertySourceIntegrationTests { |
| 36 | + |
| 37 | + @Test |
| 38 | + fun hasInjectedValues(@Autowired service: Service) { |
| 39 | + Assertions.assertThat(service.ip).isEqualTo("127.0.0.1") |
| 40 | + Assertions.assertThat(service.port).isEqualTo(4242) |
| 41 | + } |
| 42 | + |
| 43 | + @Configuration |
| 44 | + @Import(Service::class) |
| 45 | + open class Config |
| 46 | + |
| 47 | + @Component |
| 48 | + class Service(@Value("\${test.container.ip}") val ip: String, @Value("\${test.container.port}") val port: Int) |
| 49 | + |
| 50 | + class DemoContainer(val ipAddress: String = "127.0.0.1", val port: Int = 4242) |
| 51 | + |
| 52 | + companion object { |
| 53 | + |
| 54 | + @JvmStatic |
| 55 | + val container = DemoContainer() |
| 56 | + |
| 57 | + @DynamicPropertySource |
| 58 | + @JvmStatic |
| 59 | + fun containerProperties(registry: DynamicPropertyRegistry) { |
| 60 | + registry.add("test.container.ip") { container.ipAddress } |
| 61 | + registry.add("test.container.port") { container.port } |
| 62 | + } |
| 63 | + } |
| 64 | +} |
0 commit comments