Skip to content

Commit 244df31

Browse files
committed
Merge pull request #40905 from erie0210
* pr/40905: Add Kotlin example for @testcontainers Closes gh-40905
2 parents 3b09791 + 2e1ad6b commit 244df31

File tree

4 files changed

+162
-0
lines changed

4 files changed

+162
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2012-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.boot.docs.testing.testcontainers.dynamicproperties;
18+
19+
import org.junit.jupiter.api.Test
20+
import org.springframework.boot.test.context.SpringBootTest
21+
import org.springframework.test.context.DynamicPropertyRegistry
22+
import org.springframework.test.context.DynamicPropertySource
23+
import org.testcontainers.containers.Neo4jContainer
24+
import org.testcontainers.junit.jupiter.Container
25+
import org.testcontainers.junit.jupiter.Testcontainers
26+
27+
@Testcontainers
28+
@SpringBootTest
29+
class MyIntegrationTests {
30+
31+
@Test
32+
fun myTest() {
33+
// ...
34+
}
35+
36+
companion object {
37+
@Container
38+
@JvmStatic
39+
val neo4j = Neo4jContainer("neo4j:5");
40+
41+
@DynamicPropertySource
42+
@JvmStatic
43+
fun neo4jProperties(registry: DynamicPropertyRegistry) {
44+
registry.add("spring.neo4j.uri") { neo4j.boltUrl }
45+
}
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2012-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.boot.docs.testing.testcontainers.serviceconnections;
18+
19+
import org.junit.jupiter.api.Test;
20+
import org.testcontainers.containers.Neo4jContainer;
21+
import org.testcontainers.junit.jupiter.Container;
22+
import org.testcontainers.junit.jupiter.Testcontainers;
23+
24+
import org.springframework.boot.test.context.SpringBootTest;
25+
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
26+
27+
@Testcontainers
28+
@SpringBootTest
29+
class MyIntegrationTests {
30+
31+
@Test
32+
fun myTest() {
33+
// ...
34+
}
35+
36+
companion object {
37+
@Container
38+
@ServiceConnection
39+
@JvmStatic
40+
val neo4j = Neo4jContainer("neo4j:5");
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2012-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.boot.docs.testing.testcontainers.serviceconnections;
18+
19+
import org.springframework.boot.test.context.TestConfiguration
20+
import org.springframework.boot.testcontainers.service.connection.ServiceConnection
21+
import org.springframework.context.annotation.Bean
22+
import org.testcontainers.containers.GenericContainer
23+
24+
@TestConfiguration(proxyBeanMethods = false)
25+
class MyRedisConfiguration {
26+
@Bean
27+
@ServiceConnection(name = "redis")
28+
fun redisContainer(): GenericContainer<*> {
29+
return GenericContainer("redis:7")
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2012-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.boot.docs.testing.testcontainers.vanilla;
18+
19+
import org.junit.jupiter.api.Test;
20+
import org.testcontainers.containers.Neo4jContainer;
21+
import org.testcontainers.junit.jupiter.Container;
22+
import org.testcontainers.junit.jupiter.Testcontainers;
23+
24+
import org.springframework.boot.test.context.SpringBootTest;
25+
import org.springframework.boot.testcontainers.service.connection.ServiceConnection
26+
27+
@Testcontainers
28+
@SpringBootTest
29+
class MyIntegrationTests {
30+
31+
@Test
32+
fun myTest() {
33+
// ...
34+
}
35+
36+
companion object {
37+
@Container
38+
@JvmStatic
39+
val neo4j = Neo4jContainer("neo4j:5");
40+
}
41+
}
42+

0 commit comments

Comments
 (0)