complete jupiter extension for reuse of containers #11201
+2,629
−1
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.
Description
This PR implements the Named Container Providers feature for JUnit Jupiter integration, addressing the need to share expensive container instances across multiple test classes without boilerplate code.
Fixes
Closes #6401
Problem
When running integration tests across multiple test classes, each class typically starts its own container instance, even when they could share the same container. This leads to:
Solution
This PR introduces two new annotations that leverage the JUnit 5 extension API:
@ContainerProvider- Defines a named container factory method:@ContainerConfig- References a container by name in tests:Key Features
Scope.CLASS(per test class) orScope.GLOBAL(across all classes)@ContainerfieldsPerformance Impact
Before (without container reuse):
After (with container providers):
Example Usage
Base test class with shared providers:
Test classes reusing the shared container:
Changes
Core Implementation (4 new files)
ContainerProvider.java- Annotation for defining container providersContainerConfig.java- Annotation for referencing containersProviderMethod.java- Helper class for provider method metadataContainerRegistry.java- Registry managing container lifecycleModified Files (1 file)
TestcontainersExtension.java- Extended to support provider discovery, resolution, and parameter injectionTests (9 new test files)
ContainerProviderBasicTests.java- Basic functionalityContainerProviderParameterInjectionTests.java- Parameter injectionContainerProviderNewInstanceTests.java- Instance controlContainerProviderMultipleProvidersTests.java- Multiple providersContainerProviderScopeTests.java- Scope handlingContainerProviderErrorHandlingTests.java- Error scenariosContainerProviderCrossClassTests.java- Cross-class sharingContainerProviderStaticMethodTests.java- Static methodsContainerProviderMixedWithContainerTests.java- Backward compatibilityContainerProviderRealWorldExampleTests.java- Real-world exampleDocumentation
docs/test_framework_integration/junit_5.mdwith comprehensive guidedocs/examples/junit5/container-providers/README.mdwith examplesCONTAINER_PROVIDERS_FEATURE.mdwith implementation summaryExamples
examples/container-providers/demonstrating real-world usageTesting
Backward Compatibility
This feature is fully backward compatible:
@Containerfields continue to work unchangedDocumentation
Checklist
Related Issues
This PR addresses the feature request for container reuse across multiple test classes, enabling:
Note: This implementation provides a clean, declarative alternative to the manual singleton pattern while maintaining full backward compatibility with existing code.
Contribution by Gittensor, learn more at https://gittensor.io/