feat(junit-jupiter): add @SharedContainers for cross-class container sharing#11709
Closed
kimjonghun0157 wants to merge 5 commits intotestcontainers:mainfrom
Closed
feat(junit-jupiter): add @SharedContainers for cross-class container sharing#11709kimjonghun0157 wants to merge 5 commits intotestcontainers:mainfrom
kimjonghun0157 wants to merge 5 commits intotestcontainers:mainfrom
Conversation
…sharing Introduce @SharedContainers annotation and SharedContainersExtension to enable static @container fields to be shared across multiple test classes in the same JVM session. The core issue with @testcontainers is that containers are stored in a class-scoped ExtensionContext.Store, causing them to stop and restart for each test class even when declared as static fields on a shared base class. SharedContainersExtension solves this by storing containers in the root ExtensionContext.Store (context.getRoot().getStore()), which persists for the entire JVM session. Containers are started at most once and cleaned up automatically at JVM shutdown via Ryuk or JUnit's CloseableResource. Resolves: testcontainers#1441 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- Add package-private isDockerAvailable() hook to SharedContainersExtension so that unit tests can control Docker availability without a real daemon, following the same pattern as TestcontainersExtension - Add SharedContainersExtensionTests covering disabledWithoutDocker=true/false scenarios via a subclass that overrides isDockerAvailable() - Document @SharedContainers in junit_5.md under a new "Shared containers across test classes" section Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…ontainers SharedContainersExtension was missing AfterAllCallback, so containers implementing TestLifecycleAware never received afterTest() after each test class completed. Fix by: - Adding AfterAllCallback to the implements list - Storing lifecycle-aware containers in the class-level store in beforeAll() - Implementing afterAll() to retrieve and signal afterTest() to them This matches the behaviour of TestcontainersExtension for shared containers. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Update SharedContainersExtensionTests to use the doReturn(...).when(...) syntax for mocking ExtensionContext. This approach is generally preferred over when(...).thenReturn(...) to avoid unintended side effects or type-safety issues during stubbing.
…r and add lifecycle test Extract duplicate afterAll/afterEach signal logic into signalAfterTestToContainersFor() helper method, consistent with TestcontainersExtension pattern. Add SharedContainersLifecycleAwareTest to verify beforeTest()/afterTest() callbacks are correctly signalled to TestLifecycleAware shared containers. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When multiple test classes extend a common base class,
@Testcontainersrestarts containers for each class. This happens becauseTestcontainersExtensionstores containers in a class-scopedExtensionContext.Store, so evenstatic @Containerfields are stopped and restarted at every class boundary.Resolves #1441
Solution
Introduce
@SharedContainersandSharedContainersExtensionthat storestatic @Containerfields in the rootExtensionContext.Store(context.getRoot().getStore(...)). The root store persists for the entire JVM session, so containers are started at most once and cleaned up automatically viaCloseableResource/ Ryuk at JVM shutdown.Changes
SharedContainers.javadisabledWithoutDocker,paralleloptions)SharedContainersExtension.javasignalAfterTestToContainersFor()helper consistent withTestcontainersExtensionSharedContainersBaseTest.javaSharedContainersTestClassA/B.javaSharedContainersExtensionTests.javadisabledWithoutDocker/ Docker availabilitySharedContainersLifecycleAwareTest.javabeforeTest()/afterTest()callbacks onTestLifecycleAwarecontainersdocs/test_framework_integration/junit_5.mdNotes
@Containernon-static) still restart per test method, identical to@Testcontainers@SharedContainersand@Testcontainersshould not be mixed on the same class hierarchy@Testcontainers(sequential only)🤖 Generated with Claude Code