Skip to content

Commit d16630f

Browse files
committed
Document methods of starting Testcontainer containers
In this commit I have documented three methods of starting containers from Testcontainer library as part of the Spring Boot tests. Signed-off-by: Vanio Begic <[email protected]>
1 parent e884aa3 commit d16630f

File tree

7 files changed

+201
-1
lines changed

7 files changed

+201
-1
lines changed

spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/testing/testcontainers.adoc

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,47 @@ The https://www.testcontainers.org/[Testcontainers] library provides a way to ma
55
It integrates with JUnit, allowing you to write a test class that can start up a container before any of the tests run.
66
Testcontainers is especially useful for writing integration tests that talk to a real backend service such as MySQL, MongoDB, Cassandra and others.
77

8+
In following sections we will describe some of the methods you can use to integrate Testcontainers with your tests.
9+
10+
== Using via @Testcontainers JUnit5 extension
11+
12+
The Testcontainers framework provides JUnit5 extensions, which can be used to manage containers in your tests.
13+
The extension is activated by applying the
14+
815
Testcontainers can be used in a Spring Boot test as follows:
916

1017
include-code::vanilla/MyIntegrationTests[]
1118

12-
This will start up a docker container running Neo4j (if Docker is running locally) before any of the tests are run.
19+
This will start up a Docker container running Neo4j (if Docker is running locally) before any of the tests are run.
1320
In most cases, you will need to configure the application to connect to the service running in the container.
1421

22+
In this case the lifecycle of the container instance is managed by Testcontainers framework, as described in official documentation.
23+
24+
== Using via Spring managed beans
25+
26+
The containers provided by Testcontainers framework can be managed by Spring Boot as beans.
27+
This method is often used in combination with javadoc:org.springframework.boot.testcontainers.service.connection.ServiceConnection[format=annotation].
28+
29+
To use Testcontainer contains as Spring beans we need to create a configuration class declaring the container as bean:
30+
31+
include-code::beandeclaration/BeanDeclarationConfig[]
32+
33+
then we can start the container by importing the configuration class in the test class:
34+
35+
include-code::beandeclaration/SpringTest[]
36+
37+
38+
== Using via importing container declaration classes
39+
40+
A common pattern with Testcontainers framework is to declare the Container instances as static fields in an interface
41+
For example the following interface `MyInterface` declares two containers, one named `mongo` of type MongoDB and another named `neo` of type Neo4j:
42+
43+
include-code::importcontainers/MyInterface[]
44+
45+
When you have containers declared in this way, then you can have these containers managed by Spring Boot as beans.
46+
All that is needed to do that is adding javadoc:org.springframework.boot.testcontainers.context.ImportTestcontainers[format=annotation] to your configuration class as in:
1547

48+
include-code::importcontainers/MyConfiguration[]
1649

1750
[[testing.testcontainers.service-connections]]
1851
== Service Connections
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2012-2025 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.beandeclaration;
18+
19+
import org.testcontainers.containers.MongoDBContainer;
20+
import org.testcontainers.utility.DockerImageName;
21+
22+
import org.springframework.boot.test.context.TestConfiguration;
23+
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
24+
import org.springframework.context.annotation.Bean;
25+
26+
@TestConfiguration(proxyBeanMethods = false)
27+
class BeanDeclarationConfig {
28+
29+
@ServiceConnection
30+
@Bean
31+
MongoDBContainer container() {
32+
return new MongoDBContainer(DockerImageName.parse("mongo:latest"));
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2012-2025 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.beandeclaration;
18+
19+
import org.junit.jupiter.api.Test;
20+
import org.testcontainers.containers.MongoDBContainer;
21+
22+
import org.springframework.beans.factory.annotation.Autowired;
23+
import org.springframework.boot.test.context.SpringBootTest;
24+
import org.springframework.context.annotation.Import;
25+
26+
@SpringBootTest
27+
@Import(BeanDeclarationConfig.class)
28+
public class SpringTest {
29+
30+
@Autowired
31+
private MongoDBContainer mongo;
32+
33+
@Test
34+
void doTest() {
35+
System.out.println("Mongo db is running: " + this.mongo.isRunning());
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2012-2025 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.importcontainers;
18+
19+
import org.springframework.boot.test.context.TestConfiguration;
20+
import org.springframework.boot.testcontainers.context.ImportTestcontainers;
21+
22+
@TestConfiguration(proxyBeanMethods = false)
23+
@ImportTestcontainers(MyInterface.class)
24+
class MyConfiguration {
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2012-2025 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.importcontainers;
18+
19+
import org.testcontainers.containers.MongoDBContainer;
20+
import org.testcontainers.containers.Neo4jContainer;
21+
import org.testcontainers.junit.jupiter.Container;
22+
23+
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
24+
25+
interface MyInterface {
26+
27+
@Container
28+
@ServiceConnection
29+
MongoDBContainer mongoContainer = new MongoDBContainer("mongo:5.0");
30+
31+
@Container
32+
@ServiceConnection
33+
Neo4jContainer<?> neo4jContainer = new Neo4jContainer<>("neo4j:5");
34+
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.springframework.boot.docs.testing.testcontainers.beandeclaration
2+
3+
import org.springframework.boot.test.context.TestConfiguration
4+
import org.springframework.boot.testcontainers.service.connection.ServiceConnection
5+
import org.springframework.context.annotation.Bean
6+
import org.testcontainers.containers.MongoDBContainer
7+
import org.testcontainers.utility.DockerImageName
8+
9+
10+
@TestConfiguration(proxyBeanMethods = false)
11+
internal class BeanDeclarationConfig {
12+
@ServiceConnection
13+
@Bean
14+
fun container(): MongoDBContainer {
15+
return MongoDBContainer(DockerImageName.parse("mongo:latest"))
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.springframework.boot.docs.testing.testcontainers.beandeclaration
2+
3+
import org.junit.jupiter.api.Test
4+
import org.springframework.beans.factory.annotation.Autowired
5+
import org.springframework.boot.test.context.SpringBootTest
6+
import org.springframework.context.annotation.Import
7+
import org.testcontainers.containers.MongoDBContainer
8+
9+
@SpringBootTest
10+
@Import(BeanDeclarationConfig::class)
11+
class SpringTest {
12+
@Autowired
13+
private val mongo: MongoDBContainer? = null
14+
15+
@Test
16+
fun doTest() {
17+
println("Mongo db is running: " + mongo!!.isRunning)
18+
}
19+
}

0 commit comments

Comments
 (0)