You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/testing/testcontainers.adoc
+85-4
Original file line number
Diff line number
Diff line change
@@ -5,12 +5,93 @@ The https://www.testcontainers.org/[Testcontainers] library provides a way to ma
5
5
It integrates with JUnit, allowing you to write a test class that can start up a container before any of the tests run.
6
6
Testcontainers is especially useful for writing integration tests that talk to a real backend service such as MySQL, MongoDB, Cassandra and others.
7
7
8
-
Testcontainers can be used in a Spring Boot test as follows:
8
+
In following sections we will describe some of the methods you can use to integrate Testcontainers with your tests.
9
9
10
-
include-code::vanilla/MyIntegrationTests[]
11
10
12
-
This will start up a docker container running Neo4j (if Docker is running locally) before any of the tests are run.
13
-
In most cases, you will need to configure the application to connect to the service running in the container.
11
+
[[testing.testcontainers.spring-beans]]
12
+
== Using Spring Beans
13
+
14
+
The containers provided by Testcontainers can be managed by Spring Boot as beans.
15
+
16
+
To declare a container as a bean, add a javadoc:org.springframework.context.annotation.Bean[format=annotation] method to your test configuration:
17
+
18
+
include-code::MyTestConfiguration[]
19
+
20
+
You can then inject and use the container by importing the configuration class in the test class:
21
+
22
+
include-code::MyIntegrationTests[]
23
+
24
+
TIP: This method of managing containers is often used in combination with xref:#testing.testcontainers.service-connections[service connection annotations].
25
+
26
+
27
+
28
+
[[testing.testcontainers.junit-extension]]
29
+
== Using the JUnit Extension
30
+
31
+
Testcontainers provides a JUnit extension which can be used to manage containers in your tests.
32
+
The extension is activated by applying the javadoc:org.testcontainers.junit.jupiter.Testcontainers[format=annotation] annotation from Testcontainers to your test class.
33
+
34
+
You can then use the javadoc:org.testcontainers.junit.jupiter.Container[format=annotation] annotation on static container fields.
35
+
36
+
The javadoc:org.testcontainers.junit.jupiter.Testcontainers[format=annotation] annotation can be used on vanilla JUnit tests, or in combination with javadoc:org.springframework.boot.test.context.SpringBootTest[format=annotation]:
37
+
38
+
include-code::MyIntegrationTests[]
39
+
40
+
The example above will start up a Neo4j container before any of the tests are run.
41
+
The lifecycle of the container instance is managed by Testcontainers, as described in {url-testcontainers-docs}/test_framework_integration/junit_5/#extension[their official documentation].
42
+
43
+
NOTE: In most cases, you will additionally need to configure the application to connect to the service running in the container.
A common pattern with Testcontainers is to declare the container instances as static fields in an interface.
51
+
52
+
For example, the following interface declares two containers, one named `mongo` of type javadoc:org.testcontainers.containers.MongoDBContainer[] and another named `neo4j` of type javadoc:org.testcontainers.containers.Neo4jContainer.Neo4jContainer[]:
53
+
54
+
include-code::MyContainers[]
55
+
56
+
When you have containers declared in this way, you can reuse their configuration in multiple tests by having the test classes implement the interface.
57
+
58
+
It's also possible to use the same interface configuration in your Spring Boot tests.
59
+
To do so, add javadoc:org.springframework.boot.testcontainers.context.ImportTestcontainers[format=annotation] to your test configuration class:
60
+
61
+
include-code::MyTestConfiguration[]
62
+
63
+
64
+
65
+
[[testing.testcontainers.lifecycle]]
66
+
== Lifecycle of Managed Containers
67
+
68
+
If you have used the annotations and extensions provided by Testcontainers, then the lifecycle of container instances is managed entirely by Testcontainers.
69
+
Please refer to the {url-testcontainers-docs}[offical Testcontainers documentation] for the information.
70
+
71
+
When the containers are managed by Spring as beans, then their lifecycle is managed by Spring:
72
+
73
+
* Container beans are created and started before all other beans.
74
+
75
+
* Container beans are stopped after the destruction of all other beans.
76
+
77
+
This process ensures that any beans, which rely on functionality provided by the containers, can use those functionalities.
78
+
It also ensures that they are cleaned up whilst the container is still available.
79
+
80
+
TIP: When your application beans rely on functionality of containers, prefer configuring the containers as Spring beans to ensure the correct lifecycle behavior.
81
+
82
+
NOTE: Having containers managed by Testcontainers instead of as Spring beans provides no guarantee of the order in which beans and containers will shutdown.
83
+
It can happen that containers are shutdown before the beans relying on container functionality are cleaned up.
84
+
This can lead to exceptions being thrown by client beans, for example, due to loss of connection.
85
+
86
+
Container beans are created and started once per application context managed by Spring's TestContext Framework.
87
+
For details about how TestContext Framework manages the underlying application contexts and beans therein, please refer to the {url-spring-framework-docs}[Spring Framework documentation].
88
+
89
+
Container beans are stopped as part of the TestContext Framework's standard application context shutdown process.
90
+
When the application context gets shutdown, the containers are shutdown as well.
91
+
This usually happens after all tests using that specific cached application context have finished executing.
92
+
It may also happen earlier, depending on the caching behavior configured in TestContext Framework.
93
+
94
+
NOTE: A single test container instance can, and often is, retained across execution of tests from multiple test classes.
Copy file name to clipboardExpand all lines: spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/testing/testcontainers/junitextension/MyIntegrationTests.java
+3-3
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,5 @@
1
1
/*
2
-
* Copyright 2012-2024 the original author or authors.
2
+
* Copyright 2012-2025 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
Copy file name to clipboardExpand all lines: spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/testing/testcontainers/serviceconnections/MyIntegrationTests.java
+2-2
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,5 @@
1
1
/*
2
-
* Copyright 2012-2024 the original author or authors.
2
+
* Copyright 2012-2025 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
Copy file name to clipboardExpand all lines: spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/testing/testcontainers/dynamicproperties/MyIntegrationTests.kt
Copy file name to clipboardExpand all lines: spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/testing/testcontainers/junitextension/MyIntegrationTests.kt
Copy file name to clipboardExpand all lines: spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/testing/testcontainers/serviceconnections/MyIntegrationTests.kt
0 commit comments