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
SpringExtensionintegrates 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:
beforeAllin interfaceBeforeAllCallback- Throws:
Exception
afterAll
public void afterAll(ExtensionContext context) throws Exception
Delegates toTestContextManager.afterTestClass().- Specified by:
afterAllin interfaceAfterAllCallback- Throws:
Exception
postProcessTestInstance
public void postProcessTestInstance(Object testInstance, ExtensionContext context) throws Exception
Delegates toTestContextManager.prepareTestInstance(java.lang.Object).- Specified by:
postProcessTestInstancein interfaceTestInstancePostProcessor- Throws:
Exception
beforeEach
public void beforeEach(ExtensionContext context) throws Exception
- Specified by:
beforeEachin interfaceBeforeEachCallback- Throws:
Exception
beforeTestExecution
public void beforeTestExecution(ExtensionContext context) throws Exception
- Specified by:
beforeTestExecutionin 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:
afterTestExecutionin 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:
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(Constructor, Class)returnstrue. - The parameter is of type
ApplicationContextor a sub-type thereof. ParameterResolutionDelegate.isAutowirable(java.lang.reflect.Parameter, int)returnstrue.
WARNING: If a test class
Constructoris annotated with@Autowiredor automatically autowirable (seeTestConstructor), 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:
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 theParameterin the suppliedParameterContextby retrieving the corresponding dependency from the test'sApplicationContext.- Specified by:
resolveParameterin 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 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:
TestContext.getApplicationContext()