Skip to content

Commit 60dca02

Browse files
committed
Add Kotlin documentation and tests for @DynamicPropertySource
See gh-24540
1 parent 2209e7c commit 60dca02

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
}

src/docs/asciidoc/testing.adoc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4010,6 +4010,31 @@ via `@Value("${redis.host}")` and `@Value("${redis.port}")`, respectively.
40104010
40114011
}
40124012
----
4013+
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
4014+
.Kotlin
4015+
----
4016+
@SpringJUnitConfig(/* ... */)
4017+
@Testcontainers
4018+
class ExampleIntegrationTests {
4019+
4020+
companion object {
4021+
4022+
@Container
4023+
@JvmStatic
4024+
val redis: RedisContainer = RedisContainer()
4025+
4026+
@DynamicPropertySource
4027+
@JvmStatic
4028+
fun redisProperties(registry: DynamicPropertyRegistry) {
4029+
registry.add("redis.host", redis::getContainerIpAddress)
4030+
registry.add("redis.port", redis::getMappedPort)
4031+
}
4032+
}
4033+
4034+
// tests ...
4035+
4036+
}
4037+
----
40134038

40144039
[[testcontext-ctx-management-web]]
40154040
===== Loading a `WebApplicationContext`

0 commit comments

Comments
 (0)