Skip to content

Commit 8398340

Browse files
committed
Improve wording and add tips and notes where appropriate
1 parent cea2cef commit 8398340

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

buildSrc/src/main/resources/org/springframework/boot/build/antora/antora-asciidoc-attributes.properties

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ url-spring-data-rest-docs=https://docs.spring.io/spring-data/rest/reference/{ant
7575
url-spring-data-rest-site=https://spring.io/projects/spring-data-rest
7676
url-spring-data-rest-javadoc=https://docs.spring.io/spring-data/rest/docs/{dotxversion-spring-data-rest}/api
7777
url-spring-data-site=https://spring.io/projects/spring-data
78+
url-testcontainers-java-doc=https://java.testcontainers.org
7879
url-testcontainers-activemq-javadoc=https://javadoc.io/doc/org.testcontainers/activemq/{version-testcontainers-activemq}
7980
url-testcontainers-cassandra-javadoc=https://javadoc.io/doc/org.testcontainers/cassandra/{version-testcontainers-cassandra}
8081
url-testcontainers-couchbase-javadoc=https://javadoc.io/doc/org.testcontainers/couchbase/{version-testcontainers-couchbase}

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

+15-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ In this case the lifecycle of the container instance is managed by Testcontainer
2424
== Using via Spring managed beans
2525

2626
The containers provided by Testcontainers 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].
27+
This method of managing contains can be used in combination with javadoc:org.springframework.boot.testcontainers.service.connection.ServiceConnection[format=annotation].
2828

2929
To use Testcontainer contains as Spring beans we need to create a configuration class declaring the container as bean:
3030

@@ -37,7 +37,7 @@ include-code::beandeclaration/SpringTest[]
3737

3838
== Using via importing container declaration classes
3939

40-
A common pattern with Testcontainers is to declare the Container instances as static fields in an interface
40+
A common pattern with Testcontainers is to declare the Container instances as static fields in an interface.
4141
For example the following interface `MyInterface` declares two containers, one named `mongo` of type MongoDB and another named `neo` of type Neo4j:
4242

4343
include-code::importcontainers/MyInterface[]
@@ -47,22 +47,33 @@ All that is needed to do that is adding javadoc:org.springframework.boot.testcon
4747

4848
include-code::importcontainers/MyConfiguration[]
4949

50+
TIP: Using interfaces for declaring contains helps with reuse.
51+
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+
5053
== Lifecycle of managed containers
5154

5255
If you have used the annotations and extensions provided by Testcontainers, then the lifecycle of container instances is managed by the Testcontainers.
53-
Please refer to the official documentation for the information about lifecycle of the containers, when managed by the Testcontainers.
56+
Please refer to the {url-testcontainres-java-doc}[Testcontainers official documentation] for the information about lifecycle of the containers, when managed by the Testcontainers.
5457

5558
When the containers are managed by Spring as beans, then the lifecycle is clearly defined by Spring.
5659
The container beans are created and started before the beans of other types are created.
5760
This process ensures that any beans, which rely on functionality provided by the containers, can use those functionalities.
5861

5962
The test containers can be started multiple times.
6063
Like any other beans the test containers are created and started once per application context managed by the TestContext Framework.
61-
For details about how TestContext framework manages the underlying application contexts and beans therein, please refer to the official Spring documentation.
64+
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].
6265

6366
The container beans are stopped after the destruction of beans of other types.
6467
This ensures that any beans depending on the functionalities provided by the containers are cleaned up first.
6568

69+
TIP: When your application beans rely on functionality of containers, prefer configuring the containers as Spring beans.
70+
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.
71+
On shutdown the application beans depending on container functionalities are cleaned up first, and only then are the containers shut down.
72+
73+
NOTE: Having containers managed by Testcontainers instead of as Spring beans provides no guarantee of order in which beans and containers will shutdown.
74+
It can happen that containers are shutdown before the beans relying on container functionality are cleaned up.
75+
This can lead to exceptions being thrown by client beans due to loss of connection for example.
76+
6677
The containers are stopped as part of the application shutdown process, managed by the TestContext framework.
6778
When the application context gets shutdown, the containers are shutdown as well.
6879
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.

0 commit comments

Comments
 (0)