public class SpringExtension extends Object implements BeforeAllCallback, AfterAllCallback, TestInstancePostProcessor, BeforeEachCallback, AfterEachCallback, BeforeTestExecutionCallback, AfterTestExecutionCallback, ParameterResolver
SpringExtension integrates the Spring TestContext Framework
into JUnit 5's Jupiter programming model.
To use this extension, simply annotate a JUnit Jupiter based test class with
@ExtendWith(SpringExtension.class), @SpringJUnitConfig, or
@SpringJUnitWebConfig.
EnabledIf,
DisabledIf,
SpringJUnitConfig,
SpringJUnitWebConfig,
TestContextManager| Constructor and Description |
|---|
SpringExtension() |
| Modifier and Type | Method and Description |
|---|---|
void |
afterAll(ExtensionContext context)
Delegates to
TestContextManager.afterTestClass(). |
void |
afterEach(ExtensionContext context)
|
void |
afterTestExecution(ExtensionContext context)
|
void |
beforeAll(ExtensionContext context)
Delegates to
TestContextManager.beforeTestClass(). |
void |
beforeEach(ExtensionContext context)
|
void |
beforeTestExecution(ExtensionContext context)
|
static org.springframework.context.ApplicationContext |
getApplicationContext(ExtensionContext context)
Get the
ApplicationContext associated with the supplied ExtensionContext. |
void |
postProcessTestInstance(Object testInstance,
ExtensionContext context)
Delegates to
TestContextManager.prepareTestInstance(java.lang.Object). |
Object |
resolveParameter(ParameterContext parameterContext,
ExtensionContext extensionContext)
Resolve a value for the
Parameter in the supplied ParameterContext by
retrieving the corresponding dependency from the test's ApplicationContext. |
boolean |
supportsParameter(ParameterContext parameterContext,
ExtensionContext extensionContext)
Determine if the value for the
Parameter in the supplied ParameterContext
should be autowired from the test's ApplicationContext. |
public void beforeAll(ExtensionContext context) throws Exception
TestContextManager.beforeTestClass().beforeAll in interface BeforeAllCallbackExceptionpublic void afterAll(ExtensionContext context) throws Exception
TestContextManager.afterTestClass().afterAll in interface AfterAllCallbackExceptionpublic void postProcessTestInstance(Object testInstance, ExtensionContext context) throws Exception
TestContextManager.prepareTestInstance(java.lang.Object).
As of Spring Framework 5.3.2, this method also validates that test
methods and test lifecycle methods are not annotated with
@Autowired.
postProcessTestInstance in interface TestInstancePostProcessorExceptionpublic void beforeEach(ExtensionContext context) throws Exception
beforeEach in interface BeforeEachCallbackExceptionpublic void beforeTestExecution(ExtensionContext context) throws Exception
beforeTestExecution in interface BeforeTestExecutionCallbackExceptionpublic void afterTestExecution(ExtensionContext context) throws Exception
TestContextManager.afterTestExecution(java.lang.Object, java.lang.reflect.Method, java.lang.Throwable).afterTestExecution in interface AfterTestExecutionCallbackExceptionpublic void afterEach(ExtensionContext context) throws Exception
TestContextManager.afterTestMethod(java.lang.Object, java.lang.reflect.Method, java.lang.Throwable).afterEach in interface AfterEachCallbackExceptionpublic boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
Parameter in the supplied ParameterContext
should be autowired from the test's ApplicationContext.
A parameter is considered to be autowirable if one of the following
conditions is true.
Constructor and
TestConstructorUtils.isAutowirableConstructor(Constructor, Class, PropertyProvider)
returns true. Note that isAutowirableConstructor() will be
invoked with a fallback PropertyProvider that delegates its lookup
to ExtensionContext.getConfigurationParameter(String).ApplicationContext or a sub-type thereof.ApplicationEvents or a sub-type thereof.ParameterResolutionDelegate.isAutowirable(java.lang.reflect.Parameter, int) returns true.WARNING: If a test class Constructor is annotated
with @Autowired or automatically autowirable (see TestConstructor),
Spring will assume the responsibility for resolving all parameters in the
constructor. Consequently, no other registered ParameterResolver
will be able to resolve parameters.
supportsParameter in interface ParameterResolverresolveParameter(org.junit.jupiter.api.extension.ParameterContext, org.junit.jupiter.api.extension.ExtensionContext),
TestConstructorUtils.isAutowirableConstructor(Constructor, Class),
ParameterResolutionDelegate.isAutowirable(java.lang.reflect.Parameter, int)@Nullable public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
Parameter in the supplied ParameterContext by
retrieving the corresponding dependency from the test's ApplicationContext.
Delegates to ParameterResolutionDelegate.resolveDependency(java.lang.reflect.Parameter, int, java.lang.Class<?>, org.springframework.beans.factory.config.AutowireCapableBeanFactory).
resolveParameter in interface ParameterResolversupportsParameter(org.junit.jupiter.api.extension.ParameterContext, org.junit.jupiter.api.extension.ExtensionContext),
ParameterResolutionDelegate.resolveDependency(java.lang.reflect.Parameter, int, java.lang.Class<?>, org.springframework.beans.factory.config.AutowireCapableBeanFactory)public static org.springframework.context.ApplicationContext getApplicationContext(ExtensionContext context)
ApplicationContext associated with the supplied ExtensionContext.context - the current ExtensionContext (never null)IllegalStateException - if an error occurs while retrieving the application contextTestContext.getApplicationContext()