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
The example above will start up a Neo4j container before any of the tests are run.
41
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
42
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
+
43
48
NOTE: In most cases, you will additionally need to configure the application to connect to the service running in the container.
44
49
45
50
@@ -82,14 +87,15 @@ TIP: When your application beans rely on functionality of containers, prefer con
82
87
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
88
It can happen that containers are shutdown before the beans relying on container functionality are cleaned up.
84
89
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].
85
91
86
92
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].
88
94
89
95
Container beans are stopped as part of the TestContext Framework's standard application context shutdown process.
90
96
When the application context gets shutdown, the containers are shutdown as well.
91
97
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.
93
99
94
100
NOTE: A single test container instance can, and often is, retained across execution of tests from multiple test classes.
0 commit comments