Skip to content

Commit eceb603

Browse files
committed
Merge pull request #50210 from henriquejsza
* gh-35236-testcontainers-lifecycle-docs: Polish "Document Testcontainers lifecycle caveat" Document Testcontainers lifecycle caveat Closes gh-50210
2 parents 6d4a3e7 + 2df7f83 commit eceb603

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

  • spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/testing

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ include-code::MyIntegrationTests[]
4040
The example above will start up a Neo4j container before any of the tests are run.
4141
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].
4242

43+
When using the JUnit extension, container instances are stopped after the test class has run (for static fields) or after each test method (for non-static fields).
44+
This can cause issues when used with Spring Boot tests, as Spring's TestContext Framework may cache the javadoc:org.springframework.context.ApplicationContext[] beyond that point and reuse it for another test class or method with the same configuration.
45+
If the cached application context contains beans that depend on a container that has already been stopped, later tests or bean destruction callbacks may fail.
46+
For this reason, you should prefer xref:#testing.testcontainers.spring-beans[managing containers as Spring beans] or xref:#testing.testcontainers.importing-configuration-interfaces[importing container declarations] when the application context should remain usable for as long as it is cached.
47+
4348
NOTE: In most cases, you will additionally need to configure the application to connect to the service running in the container.
4449

4550

@@ -82,14 +87,15 @@ TIP: When your application beans rely on functionality of containers, prefer con
8287
NOTE: Having containers managed by Testcontainers instead of as Spring beans provides no guarantee of the order in which beans and containers will shutdown.
8388
It can happen that containers are shutdown before the beans relying on container functionality are cleaned up.
8489
This can lead to exceptions being thrown by client beans, for example, due to loss of connection.
90+
When the application context should remain usable for as long as it is cached, prefer managing containers as Spring beans or importing container declarations with javadoc:org.springframework.boot.testcontainers.context.ImportTestcontainers[format=annotation].
8591

8692
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].
93+
For details about how the TestContext Framework manages the underlying application contexts and beans therein, please refer to the {url-spring-framework-docs}[Spring Framework documentation].
8894

8995
Container beans are stopped as part of the TestContext Framework's standard application context shutdown process.
9096
When the application context gets shutdown, the containers are shutdown as well.
9197
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.
98+
It may also happen earlier, depending on the caching behavior configured in the TestContext Framework.
9399

94100
NOTE: A single test container instance can, and often is, retained across execution of tests from multiple test classes.
95101

0 commit comments

Comments
 (0)