Class SpringExtension
- java.lang.Object
- org.springframework.test.context.junit.jupiter.SpringExtension
- All Implemented Interfaces:
AfterAllCallback
,AfterEachCallback
,AfterTestExecutionCallback
,BeforeAllCallback
,BeforeEachCallback
,BeforeTestExecutionCallback
,Extension
,ParameterResolver
,TestInstancePostProcessor
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
.- Since:
- 5.0
- Author:
- Sam Brannen
- See Also:
EnabledIf
,DisabledIf
,SpringJUnitConfig
,SpringJUnitWebConfig
,TestContextManager
Constructor Summary
Constructors Constructor Description SpringExtension()
Method Summary
Constructor Detail
SpringExtension
public SpringExtension()
Method Detail
beforeAll
public void beforeAll(ExtensionContext context) throws Exception
Delegates toTestContextManager.beforeTestClass()
.- Specified by:
beforeAll
in interfaceBeforeAllCallback
- Throws:
Exception
afterAll
public void afterAll(ExtensionContext context) throws Exception
Delegates toTestContextManager.afterTestClass()
.- Specified by:
afterAll
in interfaceAfterAllCallback
- Throws:
Exception
postProcessTestInstance
public void postProcessTestInstance(Object testInstance, ExtensionContext context) throws Exception
Delegates toTestContextManager.prepareTestInstance(java.lang.Object)
.- Specified by:
postProcessTestInstance
in interfaceTestInstancePostProcessor
- Throws:
Exception
beforeEach
public void beforeEach(ExtensionContext context) throws Exception
- Specified by:
beforeEach
in interfaceBeforeEachCallback
- Throws:
Exception
beforeTestExecution
public void beforeTestExecution(ExtensionContext context) throws Exception
- Specified by:
beforeTestExecution
in interfaceBeforeTestExecutionCallback
- Throws:
Exception
afterTestExecution
public void afterTestExecution(ExtensionContext context) throws Exception
Delegates toTestContextManager.afterTestExecution(java.lang.Object, java.lang.reflect.Method, java.lang.Throwable)
.- Specified by:
afterTestExecution
in interfaceAfterTestExecutionCallback
- Throws:
Exception
afterEach
public void afterEach(ExtensionContext context) throws Exception
Delegates toTestContextManager.afterTestMethod(java.lang.Object, java.lang.reflect.Method, java.lang.Throwable)
.- 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)
returnstrue
. - The parameter is of type
ApplicationContext
or a sub-type thereof. ParameterResolutionDelegate.isAutowirable(java.lang.reflect.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:
resolveParameter(org.junit.jupiter.api.extension.ParameterContext, org.junit.jupiter.api.extension.ExtensionContext)
,TestConstructorUtils.isAutowirableConstructor(Constructor, Class)
,ParameterResolutionDelegate.isAutowirable(java.lang.reflect.Parameter, int)
- The declaring executable is a
resolveParameter
@Nullable public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
Resolve a value for theParameter
in the suppliedParameterContext
by retrieving the corresponding dependency from the test'sApplicationContext
.- Specified by:
resolveParameter
in interfaceParameterResolver
- See Also:
supportsParameter(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)
getApplicationContext
public static ApplicationContext getApplicationContext(ExtensionContext context)
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:
TestContext.getApplicationContext()