Polish 'Document methods of starting Testcontainer containers'

See gh-44187
This commit is contained in:
Phillip Webb
2025-05-05 22:03:01 -07:00
parent 4a98b1787f
commit 8814d8a7dc
18 changed files with 216 additions and 122 deletions

View File

@@ -14,22 +14,18 @@
* limitations under the License.
*/
package org.springframework.boot.docs.testing.testcontainers.importcontainers;
package org.springframework.boot.docs.testing.testcontainers.importingconfigurationinterfaces;
import org.testcontainers.containers.MongoDBContainer;
import org.testcontainers.containers.Neo4jContainer;
import org.testcontainers.junit.jupiter.Container;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
interface MyInterface {
interface MyContainers {
@Container
@ServiceConnection
MongoDBContainer mongoContainer = new MongoDBContainer("mongo:5.0");
@Container
@ServiceConnection
Neo4jContainer<?> neo4jContainer = new Neo4jContainer<>("neo4j:5");
}

View File

@@ -14,13 +14,13 @@
* limitations under the License.
*/
package org.springframework.boot.docs.testing.testcontainers.importcontainers;
package org.springframework.boot.docs.testing.testcontainers.importingconfigurationinterfaces;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.testcontainers.context.ImportTestcontainers;
@TestConfiguration(proxyBeanMethods = false)
@ImportTestcontainers(MyInterface.class)
class MyConfiguration {
@ImportTestcontainers(MyContainers.class)
class MyTestConfiguration {
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.boot.docs.testing.testcontainers.vanilla;
package org.springframework.boot.docs.testing.testcontainers.junitextension;
import org.junit.jupiter.api.Test;
import org.testcontainers.containers.Neo4jContainer;
@@ -32,7 +32,7 @@ class MyIntegrationTests {
@Test
void myTest() {
// ...
/**/ System.out.println(neo4j);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -34,7 +34,7 @@ class MyIntegrationTests {
@Test
void myTest() {
// ...
/**/ System.out.println(neo4j);
}
}

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.boot.docs.testing.testcontainers.beandeclaration;
package org.springframework.boot.docs.testing.testcontainers.springbeans;
import org.junit.jupiter.api.Test;
import org.testcontainers.containers.MongoDBContainer;
@@ -24,15 +24,15 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Import;
@SpringBootTest
@Import(BeanDeclarationConfig.class)
class SpringTest {
@Import(MyTestConfiguration.class)
class MyIntegrationTests {
@Autowired
private MongoDBContainer mongo;
@Test
void doTest() {
System.out.println("Mongo db is running: " + this.mongo.isRunning());
void myTest() {
/**/ System.out.println(this.mongo);
}
}

View File

@@ -14,22 +14,20 @@
* limitations under the License.
*/
package org.springframework.boot.docs.testing.testcontainers.beandeclaration;
package org.springframework.boot.docs.testing.testcontainers.springbeans;
import org.testcontainers.containers.MongoDBContainer;
import org.testcontainers.utility.DockerImageName;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.context.annotation.Bean;
@TestConfiguration(proxyBeanMethods = false)
class BeanDeclarationConfig {
class MyTestConfiguration {
@ServiceConnection
@Bean
MongoDBContainer container() {
return new MongoDBContainer(DockerImageName.parse("mongo:latest"));
MongoDBContainer mongoDbContainer() {
return new MongoDBContainer(DockerImageName.parse("mongo:5.0"));
}
}

View File

@@ -1,17 +0,0 @@
package org.springframework.boot.docs.testing.testcontainers.beandeclaration
import org.springframework.boot.test.context.TestConfiguration
import org.springframework.boot.testcontainers.service.connection.ServiceConnection
import org.springframework.context.annotation.Bean
import org.testcontainers.containers.MongoDBContainer
import org.testcontainers.utility.DockerImageName
@TestConfiguration(proxyBeanMethods = false)
internal class BeanDeclarationConfig {
@ServiceConnection
@Bean
fun container(): MongoDBContainer {
return MongoDBContainer(DockerImageName.parse("mongo:latest"))
}
}

View File

@@ -1,19 +0,0 @@
package org.springframework.boot.docs.testing.testcontainers.beandeclaration
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.context.annotation.Import
import org.testcontainers.containers.MongoDBContainer
@SpringBootTest
@Import(BeanDeclarationConfig::class)
class SpringTest {
@Autowired
private val mongo: MongoDBContainer? = null
@Test
fun doTest() {
println("Mongo db is running: " + mongo!!.isRunning)
}
}

View File

@@ -29,7 +29,7 @@ class MyIntegrationTests {
@Test
fun myTest() {
// ...
/**/ println()
}
companion object {

View File

@@ -0,0 +1,35 @@
/*
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.docs.testing.testcontainers.importingconfigurationinterfaces
import org.testcontainers.containers.MongoDBContainer
import org.testcontainers.containers.Neo4jContainer
import org.testcontainers.junit.jupiter.Container
interface MyContainers {
companion object {
@Container
val mongoContainer: MongoDBContainer = MongoDBContainer("mongo:5.0")
@Container
val neo4jContainer: Neo4jContainer<*> = Neo4jContainer("neo4j:5")
}
}

View File

@@ -0,0 +1,26 @@
/*
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.docs.testing.testcontainers.importingconfigurationinterfaces
import org.springframework.boot.test.context.TestConfiguration
import org.springframework.boot.testcontainers.context.ImportTestcontainers
@TestConfiguration(proxyBeanMethods = false)
@ImportTestcontainers(MyContainers::class)
class MyTestConfiguration {
}

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.boot.docs.testing.testcontainers.vanilla
package org.springframework.boot.docs.testing.testcontainers.junitextension
import org.junit.jupiter.api.Test;
import org.testcontainers.containers.Neo4jContainer;
@@ -22,7 +22,6 @@ import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection
@Testcontainers
@SpringBootTest
@@ -30,13 +29,15 @@ class MyIntegrationTests {
@Test
fun myTest() {
// ...
/**/ println()
}
companion object {
@Container
@JvmStatic
val neo4j = Neo4jContainer("neo4j:5");
}
}

View File

@@ -30,7 +30,7 @@ class MyIntegrationTests {
@Test
fun myTest() {
// ...
/**/ println()
}
companion object {

View File

@@ -23,9 +23,11 @@ import org.testcontainers.containers.GenericContainer
@TestConfiguration(proxyBeanMethods = false)
class MyRedisConfiguration {
@Bean
@ServiceConnection(name = "redis")
fun redisContainer(): GenericContainer<*> {
return GenericContainer("redis:7")
}
}

View File

@@ -0,0 +1,36 @@
/*
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.docs.testing.testcontainers.springbeans
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.context.annotation.Import
import org.testcontainers.containers.MongoDBContainer
@SpringBootTest
@Import(MyTestConfiguration::class)
class MyIntegrationTests {
@Autowired
private val mongo: MongoDBContainer? = null
@Test
fun myTest() {
/**/ println()
}
}

View File

@@ -0,0 +1,31 @@
/*
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.docs.testing.testcontainers.springbeans
import org.springframework.boot.test.context.TestConfiguration
import org.springframework.context.annotation.Bean
import org.testcontainers.containers.MongoDBContainer
import org.testcontainers.utility.DockerImageName
@TestConfiguration(proxyBeanMethods = false)
class MyTestConfiguration {
@Bean
fun mongoDbContainer(): MongoDBContainer {
return MongoDBContainer(DockerImageName.parse("mongo:5.0"))
}
}