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 Summary
Nested classes/interfaces inherited from interface TestInstantiationAwareExtension
TestInstantiationAwareExtension.ExtensionContextScope -
Constructor Summary
Constructors -
Method Summary
Modifier 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
-
SpringExtension
public SpringExtension()
-
-
Method Details
-
getTestInstantiationExtensionContextScope
public 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 interfaceTestInstantiationAwareExtension- Since:
- 7.0
- See Also:
-
beforeAll
Delegates toTestContextManager.beforeTestClass().- Specified by:
beforeAllin interfaceBeforeAllCallback- Throws:
Exception
-
afterAll
Delegates toTestContextManager.afterTestClass().- Specified by:
afterAllin interfaceAfterAllCallback- Throws:
Exception
-
postProcessTestInstance
Delegates toTestContextManager.prepareTestInstance(Object).This method also validates that test methods and test lifecycle methods are not annotated with
@Autowired.- Specified by:
postProcessTestInstancein interfaceTestInstancePostProcessor- Throws:
Exception
-
beforeEach
Delegates toTestContextManager.beforeTestMethod(Object, Method).- Specified by:
beforeEachin interfaceBeforeEachCallback- Throws:
Exception
-
beforeTestExecution
Delegates toTestContextManager.beforeTestExecution(Object, Method).- Specified by:
beforeTestExecutionin interfaceBeforeTestExecutionCallback- Throws:
Exception
-
afterTestExecution
- Specified by:
afterTestExecutionin interfaceAfterTestExecutionCallback- Throws:
Exception
-
afterEach
- Specified by:
afterEachin interfaceAfterEachCallback- Throws:
Exception
-
supportsParameter
public 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)returnstrue.
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 interfaceParameterResolver- See Also:
- The declaring
executable is a
-
resolveParameter
public @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 interfaceParameterResolver- See Also:
-
getApplicationContext
Get theApplicationContextassociated with the suppliedExtensionContext.- Parameters:
context- the currentExtensionContext(nevernull)- Returns:
- the application context
- Throws:
IllegalStateException- if an error occurs while retrieving the application context- See Also:
-