public class SpringExtension extends java.lang.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 ApplicationContext |
getApplicationContext(ExtensionContext context)
Get the
ApplicationContext associated with the supplied ExtensionContext . |
void |
postProcessTestInstance(java.lang.Object testInstance,
ExtensionContext context)
Delegates to
TestContextManager.prepareTestInstance(java.lang.Object) . |
java.lang.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 java.lang.Exception
TestContextManager.beforeTestClass()
.beforeAll
in interface BeforeAllCallback
java.lang.Exception
public void afterAll(ExtensionContext context) throws java.lang.Exception
TestContextManager.afterTestClass()
.afterAll
in interface AfterAllCallback
java.lang.Exception
public void postProcessTestInstance(java.lang.Object testInstance, ExtensionContext context) throws java.lang.Exception
TestContextManager.prepareTestInstance(java.lang.Object)
.postProcessTestInstance
in interface TestInstancePostProcessor
java.lang.Exception
public void beforeEach(ExtensionContext context) throws java.lang.Exception
beforeEach
in interface BeforeEachCallback
java.lang.Exception
public void beforeTestExecution(ExtensionContext context) throws java.lang.Exception
beforeTestExecution
in interface BeforeTestExecutionCallback
java.lang.Exception
public void afterTestExecution(ExtensionContext context) throws java.lang.Exception
TestContextManager.afterTestExecution(java.lang.Object, java.lang.reflect.Method, java.lang.Throwable)
.afterTestExecution
in interface AfterTestExecutionCallback
java.lang.Exception
public void afterEach(ExtensionContext context) throws java.lang.Exception
TestContextManager.afterTestMethod(java.lang.Object, java.lang.reflect.Method, java.lang.Throwable)
.afterEach
in interface AfterEachCallback
java.lang.Exception
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
Parameter
in the supplied ParameterContext
should be autowired from the test's ApplicationContext
.
Returns true
if the parameter is declared in a Constructor
that is annotated with @Autowired
and otherwise delegates to
ParameterAutowireUtils.isAutowirable(java.lang.reflect.Parameter, int)
.
WARNING: If the parameter is declared in a Constructor
that is annotated with @Autowired
, 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 ParameterResolver
resolveParameter(org.junit.jupiter.api.extension.ParameterContext, org.junit.jupiter.api.extension.ExtensionContext)
,
ParameterAutowireUtils.isAutowirable(java.lang.reflect.Parameter, int)
@Nullable public java.lang.Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
Parameter
in the supplied ParameterContext
by
retrieving the corresponding dependency from the test's ApplicationContext
.
Delegates to ParameterAutowireUtils.resolveDependency(java.lang.reflect.Parameter, int, java.lang.Class<?>, org.springframework.context.ApplicationContext)
.
resolveParameter
in interface ParameterResolver
supportsParameter(org.junit.jupiter.api.extension.ParameterContext, org.junit.jupiter.api.extension.ExtensionContext)
,
ParameterAutowireUtils.resolveDependency(java.lang.reflect.Parameter, int, java.lang.Class<?>, org.springframework.context.ApplicationContext)
public static ApplicationContext getApplicationContext(ExtensionContext context)
ApplicationContext
associated with the supplied ExtensionContext
.context
- the current ExtensionContext
(never null
)java.lang.IllegalStateException
- if an error occurs while retrieving the application contextTestContext.getApplicationContext()