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 TypeMethodDescriptionvoid
afterAll
(ExtensionContext context) Delegates toTestContextManager.afterTestClass()
.void
afterEach
(ExtensionContext context) void
afterTestExecution
(ExtensionContext context) void
beforeAll
(ExtensionContext context) Delegates toTestContextManager.beforeTestClass()
.void
beforeEach
(ExtensionContext context) Delegates toTestContextManager.beforeTestMethod(Object, Method)
.void
beforeTestExecution
(ExtensionContext context) Delegates toTestContextManager.beforeTestExecution(Object, Method)
.static ApplicationContext
getApplicationContext
(ExtensionContext context) Get theApplicationContext
associated with the suppliedExtensionContext
.void
postProcessTestInstance
(Object testInstance, ExtensionContext context) Delegates toTestContextManager.prepareTestInstance(Object)
.resolveParameter
(ParameterContext parameterContext, ExtensionContext extensionContext) Resolve a value for theParameter
in the suppliedParameterContext
by retrieving the corresponding dependency from the test'sApplicationContext
.boolean
supportsParameter
(ParameterContext parameterContext, ExtensionContext extensionContext) Determine if the value for theParameter
in the suppliedParameterContext
should be autowired from the test'sApplicationContext
.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface TestInstantiationAwareExtension
getTestInstantiationExtensionContextScope
-
Constructor Details
-
SpringExtension
public SpringExtension()
-
-
Method Details
-
beforeAll
Delegates toTestContextManager.beforeTestClass()
.- Specified by:
beforeAll
in interfaceBeforeAllCallback
- Throws:
Exception
-
afterAll
Delegates toTestContextManager.afterTestClass()
.- Specified by:
afterAll
in 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:
postProcessTestInstance
in interfaceTestInstancePostProcessor
- Throws:
Exception
-
beforeEach
Delegates toTestContextManager.beforeTestMethod(Object, Method)
.- Specified by:
beforeEach
in interfaceBeforeEachCallback
- Throws:
Exception
-
beforeTestExecution
Delegates toTestContextManager.beforeTestExecution(Object, Method)
.- Specified by:
beforeTestExecution
in interfaceBeforeTestExecutionCallback
- Throws:
Exception
-
afterTestExecution
- Specified by:
afterTestExecution
in interfaceAfterTestExecutionCallback
- Throws:
Exception
-
afterEach
- Specified by:
afterEach
in interfaceAfterEachCallback
- Throws:
Exception
-
supportsParameter
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) Determine if the value for theParameter
in the suppliedParameterContext
should 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
Constructor
andTestConstructorUtils.isAutowirableConstructor(Constructor, Class, PropertyProvider)
returnstrue
. Note thatisAutowirableConstructor()
will be invoked with a fallbackPropertyProvider
that delegates its lookup toExtensionContext.getConfigurationParameter(String)
. - The parameter is of type
ApplicationContext
or a sub-type thereof. - The parameter is of type
ApplicationEvents
or a sub-type thereof. ParameterResolutionDelegate.isAutowirable(Parameter, int)
returnstrue
.
WARNING: If a test class
Constructor
is annotated with@Autowired
or automatically autowirable (seeTestConstructor
), Spring will assume the responsibility for resolving all parameters in the constructor. Consequently, no other registeredParameterResolver
will be able to resolve parameters.- Specified by:
supportsParameter
in interfaceParameterResolver
- See Also:
- The declaring
executable is a
-
resolveParameter
public @Nullable Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) Resolve a value for theParameter
in the suppliedParameterContext
by retrieving the corresponding dependency from the test'sApplicationContext
.Delegates to
ParameterResolutionDelegate.resolveDependency(Parameter, int, Class, AutowireCapableBeanFactory)
.- Specified by:
resolveParameter
in interfaceParameterResolver
- See Also:
-
getApplicationContext
Get theApplicationContext
associated 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:
-