Skip to content

Commit 69ddf58

Browse files
henriquejszasnicoll
authored andcommitted
Document Testcontainers lifecycle caveat
Document how Testcontainers-managed static containers can be stopped before Spring's cached application context is closed or reused. See gh-50210 Signed-off-by: henriquejsza <henriquejsza@gmail.com>
1 parent 6d4a3e7 commit 69ddf58

1 file changed

Lines changed: 5 additions & 0 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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ NOTE: Having containers managed by Testcontainers instead of as Spring beans pro
8383
It can happen that containers are shutdown before the beans relying on container functionality are cleaned up.
8484
This can lead to exceptions being thrown by client beans, for example, due to loss of connection.
8585

86+
WARNING: When using Testcontainers' JUnit extension with static fields annotated with javadoc:org.testcontainers.junit.jupiter.Container[format=annotation], container instances are stopped after the test class has run.
87+
Spring's TestContext Framework may cache the javadoc:org.springframework.context.ApplicationContext[] beyond that point and reuse it for another test class with the same configuration.
88+
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.
89+
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].
90+
8691
Container beans are created and started once per application context managed by Spring's TestContext Framework.
8792
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].
8893

0 commit comments

Comments
 (0)