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
+52-47Lines changed: 52 additions & 47 deletions
Original file line number
Diff line number
Diff line change
@@ -8,85 +8,90 @@ Testcontainers is especially useful for writing integration tests that talk to a
8
8
In following sections we will describe some of the methods you can use to integrate Testcontainers with your tests.
9
9
10
10
11
+
[[testing.testcontainers.spring-beans]]
12
+
== Using Spring Beans
11
13
12
-
[[testing.testcontainers.via-junit-extension]]
13
-
== Using via @Testcontainers JUnit5 extension
14
+
The containers provided by Testcontainers can be managed by Spring Boot as beans.
14
15
15
-
The Testcontainers provides JUnit5 extensions, which can be used to manage containers in your tests.
16
-
The extension is activated by applying the `@Testcontainers` annotation from Testcontainers to your test class.
16
+
To declare a container as a bean, add a javadoc:org.springframework.context.annotation.Bean[format=annotation] method to your test configuration:
17
17
18
-
Testcontainers can be used in a Spring Boot test as follows:
18
+
include-code::MyTestConfiguration[]
19
19
20
-
include-code::vanilla/MyIntegrationTests[]
20
+
You can then inject and use the container by importing the configuration class in the test class:
21
21
22
-
This will start up a Docker container running Neo4j (if Docker is running locally) before any of the tests are run.
23
-
In most cases, you will need to configure the application to connect to the service running in the container.
22
+
include-code::MyIntegrationTests[]
24
23
25
-
In this case the lifecycle of the container instance is managed by Testcontainers, as described in official documentation.
24
+
TIP: This method of managing containers is often used in combination with xref:#testing.testcontainers.service-connections[service connection annotations].
26
25
27
26
28
-
[[testing.testcontainers.via-spring-beans]]
29
-
== Using via Spring managed beans
30
27
31
-
The containers provided by Testcontainers can be managed by Spring Boot as beans.
32
-
This method of managing contains can be used in combination with javadoc:org.springframework.boot.testcontainers.service.connection.ServiceConnection[format=annotation].
28
+
[[testing.testcontainers.junit-extension]]
29
+
== Using the JUnit Extension
33
30
34
-
To use Testcontainer contains as Spring beans we need to create a configuration class declaring the container as bean:
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.
You can then use the javadoc:org.testcontainers.junit.jupiter.Container[format=annotation] annotation on static container fields.
37
35
38
-
then we can start the container by importing the configuration class in the test class:
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]:
39
37
40
-
include-code::beandeclaration/SpringTest[]
38
+
include-code::MyIntegrationTests[]
41
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].
== Using via importing container declaration classes
43
+
NOTE: In most cases, you will additionally need to configure the application to connect to the service running in the container.
45
44
46
-
A common pattern with Testcontainers is to declare the Container instances as static fields in an interface.
47
-
For example the following interface `MyInterface` declares two containers, one named `mongo` of type MongoDB and another named `neo` of type Neo4j:
48
45
49
-
include-code::importcontainers/MyInterface[]
50
46
51
-
When you have containers declared in this way, then you can have these containers managed by Spring Boot as beans.
52
-
All that is needed to do that is adding javadoc:org.springframework.boot.testcontainers.context.ImportTestcontainers[format=annotation] to your configuration class as in:
A common pattern with Testcontainers is to declare the container instances as static fields in an interface.
55
51
56
-
TIP: Using interfaces for declaring contains helps with reuse.
57
-
When containers are declared in an interface, this can be reused in your javadoc:org.springframework.context.annotation.Configuration[format=annotation] classes and in test classes.
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[]:
58
53
54
+
include-code::MyContainers[]
59
55
60
-
[[test.testcontainers.container-lifecycle]]
61
-
== Lifecycle of managed containers
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
62
63
-
If you have used the annotations and extensions provided by Testcontainers, then the lifecycle of container instances is managed by the Testcontainers.
64
-
Please refer to the {url-testcontainres-java-doc}[Testcontainers official documentation] for the information about lifecycle of the containers, when managed by the Testcontainers.
65
63
66
-
When the containers are managed by Spring as beans, then the lifecycle is clearly defined by Spring.
67
-
The container beans are created and started before the beans of other types are created.
68
-
This process ensures that any beans, which rely on functionality provided by the containers, can use those functionalities.
69
64
70
-
The test containers can be started multiple times.
71
-
Like any other beans the test containers are created and started once per application context managed by the TestContext Framework.
72
-
For details about how TestContext framework manages the underlying application contexts and beans therein, please refer to the {url-spring-framework-docs}[official Spring documentation].
65
+
[[testing.testcontainers.lifecycle]]
66
+
== Lifecycle of Managed Containers
73
67
74
-
The container beans are stopped after the destruction of beans of other types.
75
-
This ensures that any beans depending on the functionalities provided by the containers are cleaned up first.
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.
76
70
77
-
TIP: When your application beans rely on functionality of containers, prefer configuring the containers as Spring beans.
78
-
When containers are managed as Spring beans, then Spring framework ensures that upon start the container beans are started before any beans relying on them.
79
-
On shutdown the application beans depending on container functionalities are cleaned up first, and only then are the containers shut down.
71
+
When the containers are managed by Spring as beans, then their lifecycle is managed by Spring:
80
72
81
-
NOTE: Having containers managed by Testcontainers instead of as Spring beans provides no guarantee of order in which beans and containers will shutdown.
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.
82
83
It can happen that containers are shutdown before the beans relying on container functionality are cleaned up.
83
-
This can lead to exceptions being thrown by client beans due to loss of connection for example.
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].
84
88
85
-
The containers are stopped as part of the application shutdown process, managed by the TestContext framework.
89
+
Container beans are stopped as part of the TestContext Framework's standard application context shutdown process.
86
90
When the application context gets shutdown, the containers are shutdown as well.
87
-
This usually happens after all tests using that specific cached application context have finished executing, but may happen earlier depending on the caching behavior configured in TestContext Framework.
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.
88
93
89
-
It is important to note that a single test container instance can be, and often is, retained across execution of tests from multiple test classes.
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/importingconfigurationinterfaces/MyContainers.java
Copy file name to clipboardExpand all lines: spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/testing/testcontainers/importingconfigurationinterfaces/MyTestConfiguration.java
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-3Lines changed: 3 additions & 3 deletions
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-2Lines changed: 2 additions & 2 deletions
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/springbeans/MyIntegrationTests.java
Copy file name to clipboardExpand all lines: spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/testing/testcontainers/springbeans/MyTestConfiguration.java
Copy file name to clipboardExpand all lines: spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/testing/testcontainers/beandeclaration/BeanDeclarationConfig.kt
Copy file name to clipboardExpand all lines: spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/testing/testcontainers/beandeclaration/SpringTest.kt
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
0 commit comments