Class SpringExtension

java.lang.Object
org.springframework.test.context.junit.jupiter.SpringExtension
All Implemented Interfaces:
AfterAllCallback, AfterEachCallback, AfterTestExecutionCallback, BeforeAllCallback, BeforeEachCallback, BeforeTestExecutionCallback, Extension, ParameterResolver, TestInstancePostProcessor, TestInstantiationAwareExtension

SpringExtension integrates the Spring TestContext Framework into the JUnit Jupiter testing framework.

To use this extension, annotate a JUnit Jupiter based test class with @ExtendWith(SpringExtension.class), @SpringJUnitConfig, @SpringJUnitWebConfig, or any other annotation that is meta-annotated with @ExtendWith(SpringExtension.class) such as @SpringBootTest, etc.

As of Spring Framework 7.0, the SpringExtension is configured to use a test-method scoped ExtensionContext, which enables consistent dependency injection into fields and constructors from the ApplicationContext for the current test method in a @Nested test class hierarchy. However, if a third-party TestExecutionListener is not compatible with the semantics associated with a test-method scoped extension context — or if a developer wishes to switch to test-class scoped semantics — the SpringExtension can be configured by annotating a top-level test class with @SpringExtensionConfig(useTestClassScopedExtensionContext = true) or by setting the "spring.test.extension.context.scope" property to test_class. Note that an explicit @SpringExtensionConfig declaration overrides the globally configured property. Furthermore, the SpringExtension will always use a test-class scoped ExtensionContext for a test class that is configured to use JUnit Jupiter’s @TestInstance(Lifecycle.PER_CLASS) semantics.

NOTE: This class requires JUnit Jupiter 6.0 or higher.

Since:
5.0
Author:
Sam Brannen, Simon Baslé
See Also: