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, simply annotate a JUnit Jupiter based test class with
@ExtendWith(SpringExtension.class), @SpringJUnitConfig, or
@SpringJUnitWebConfig.
- 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.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.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface TestInstantiationAwareExtension
getTestInstantiationExtensionContextScope
-
Constructor Details
-
SpringExtension
public SpringExtension()
-
-
Method Details
-
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:
-