-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Description
When using maven-surefire-plugin or maven-failsafe-plugin with activated forking (https://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html#forkCount + https://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html#reuseForks) a new JVM might be spawned for every test. This will prevent the pattern from working as intended as every JVM instance does its own classloading and thus "reloads" the abstract class containing the static container instance.
The only solution is not to fork at all or to only fork once and resuse the JVM for all tests (forkCount=1 + reuseForks=true).
If forking is mandatory for any reason (e.g. isolating testcases) there currently seems no way to use the pattern described here
https://www.testcontainers.org/test_framework_integration/manual_lifecycle_control/#singleton-containers
As it took me quite some time to find the underlying issue a small hint concerning forking during tests in the description of the pattern might save others some headache.
Having a standard mechanism of declaring singleton containers that even work with forking/multiple JVMs of course would be even better - a possible approach was discussed here: #1441
I assume there might be the need for a central entity (container?) orchestrating the communication/access of all the tests to the containers that are decoupled from the lifetime of the instantiating JVM, as well as the lifecycle of those containers.
One could of course argue that using containers like this in the context of tests is an anti-pattern.
But depending on the requirements (e.g. performance) or the effort to restructure the test-suite to allow for small, fast, atomar containers singleton containers might be the only viable approach, or at least a starting point for a better solution.