Class 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 to use a test-class scoped extension context by annotating a
top-level test class with
@SpringExtensionConfig(useTestClassScopedExtensionContext = true).
- Since:
- 5.0
- Author:
- Sam Brannen, Simon Baslé
- See Also:
- 
Nested Class SummaryNested classes/interfaces inherited from interface TestInstantiationAwareExtensionTestInstantiationAwareExtension.ExtensionContextScope
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionvoidafterAll(ExtensionContext context) Delegates toTestContextManager.afterTestClass().voidafterEach(ExtensionContext context) voidafterTestExecution(ExtensionContext context) voidbeforeAll(ExtensionContext context) Delegates toTestContextManager.beforeTestClass().voidbeforeEach(ExtensionContext context) Delegates toTestContextManager.beforeTestMethod(Object, Method).voidbeforeTestExecution(ExtensionContext context) Delegates toTestContextManager.beforeTestExecution(Object, Method).static ApplicationContextgetApplicationContext(ExtensionContext context) Get theApplicationContextassociated with the suppliedExtensionContext.ReturnsExtensionContextScope.TEST_METHOD.voidpostProcessTestInstance(Object testInstance, ExtensionContext context) Delegates toTestContextManager.prepareTestInstance(Object).resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) Resolve a value for theParameterin the suppliedParameterContextby retrieving the corresponding dependency from the test'sApplicationContext.booleansupportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) Determine if the value for theParameterin the suppliedParameterContextshould be autowired from the test'sApplicationContext.
- 
Constructor Details- 
SpringExtensionpublic SpringExtension()
 
- 
- 
Method Details- 
getTestInstantiationExtensionContextScopepublic TestInstantiationAwareExtension.ExtensionContextScope getTestInstantiationExtensionContextScope(ExtensionContext rootContext) ReturnsExtensionContextScope.TEST_METHOD.This can be effectively overridden by annotating a test class with @SpringExtensionConfig(useTestClassScopedExtensionContext = true). See theclass-level Javadocfor further details.- Specified by:
- getTestInstantiationExtensionContextScopein interface- TestInstantiationAwareExtension
- Since:
- 7.0
- See Also:
 
- 
beforeAllDelegates toTestContextManager.beforeTestClass().- Specified by:
- beforeAllin interface- BeforeAllCallback
- Throws:
- Exception
 
- 
afterAllDelegates toTestContextManager.afterTestClass().- Specified by:
- afterAllin interface- AfterAllCallback
- Throws:
- Exception
 
- 
postProcessTestInstanceDelegates toTestContextManager.prepareTestInstance(Object).This method also validates that test methods and test lifecycle methods are not annotated with @Autowired.- Specified by:
- postProcessTestInstancein interface- TestInstancePostProcessor
- Throws:
- Exception
 
- 
beforeEachDelegates toTestContextManager.beforeTestMethod(Object, Method).- Specified by:
- beforeEachin interface- BeforeEachCallback
- Throws:
- Exception
 
- 
beforeTestExecutionDelegates toTestContextManager.beforeTestExecution(Object, Method).- Specified by:
- beforeTestExecutionin interface- BeforeTestExecutionCallback
- Throws:
- Exception
 
- 
afterTestExecution- Specified by:
- afterTestExecutionin interface- AfterTestExecutionCallback
- Throws:
- Exception
 
- 
afterEach- Specified by:
- afterEachin interface- AfterEachCallback
- Throws:
- Exception
 
- 
supportsParameterpublic boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) Determine if the value for theParameterin the suppliedParameterContextshould be autowired from the test'sApplicationContext.A parameter is considered to be autowirable if one of the following conditions is true.- The declaring
executable is a ConstructorandTestConstructorUtils.isAutowirableConstructor(Executable, PropertyProvider)returnstrue. Note thatisAutowirableConstructor()will be invoked with a fallbackPropertyProviderthat delegates its lookup toExtensionContext.getConfigurationParameter(String).
- The parameter is of type ApplicationContextor a sub-type thereof.
- The parameter is of type ApplicationEventsor a sub-type thereof.
- ParameterResolutionDelegate.isAutowirable(Parameter, int)returns- true.
 WARNING: If a test class Constructoris annotated with@Autowiredor automatically autowirable (see@TestConstructor), Spring will assume the responsibility for resolving all parameters in the constructor. Consequently, no other registeredParameterResolverwill be able to resolve parameters.- Specified by:
- supportsParameterin interface- ParameterResolver
- See Also:
 
- The declaring
executable is a 
- 
resolveParameterpublic @Nullable Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) Resolve a value for theParameterin the suppliedParameterContextby retrieving the corresponding dependency from the test'sApplicationContext.Delegates to ParameterResolutionDelegate.resolveDependency(Parameter, int, Class, AutowireCapableBeanFactory).- Specified by:
- resolveParameterin interface- ParameterResolver
- See Also:
 
- 
getApplicationContextGet theApplicationContextassociated with the suppliedExtensionContext.- Parameters:
- context- the current- ExtensionContext(never- null)
- Returns:
- the application context
- Throws:
- IllegalStateException- if an error occurs while retrieving the application context
- See Also:
 
 
-