Class SpringExtension
java.lang.Object
org.springframework.test.context.junit.jupiter.SpringExtension
- All Implemented Interfaces:
- org.junit.jupiter.api.extension.AfterAllCallback,- org.junit.jupiter.api.extension.AfterEachCallback,- org.junit.jupiter.api.extension.AfterTestExecutionCallback,- org.junit.jupiter.api.extension.BeforeAllCallback,- org.junit.jupiter.api.extension.BeforeEachCallback,- org.junit.jupiter.api.extension.BeforeTestExecutionCallback,- org.junit.jupiter.api.extension.Extension,- org.junit.jupiter.api.extension.ParameterResolver,- org.junit.jupiter.api.extension.TestInstancePostProcessor
public class SpringExtension
extends Object
implements org.junit.jupiter.api.extension.BeforeAllCallback, org.junit.jupiter.api.extension.AfterAllCallback, org.junit.jupiter.api.extension.TestInstancePostProcessor, org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.AfterEachCallback, org.junit.jupiter.api.extension.BeforeTestExecutionCallback, org.junit.jupiter.api.extension.AfterTestExecutionCallback, org.junit.jupiter.api.extension.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:
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionvoidafterAll(org.junit.jupiter.api.extension.ExtensionContext context) Delegates toTestContextManager.afterTestClass().voidafterEach(org.junit.jupiter.api.extension.ExtensionContext context) voidafterTestExecution(org.junit.jupiter.api.extension.ExtensionContext context) voidbeforeAll(org.junit.jupiter.api.extension.ExtensionContext context) Delegates toTestContextManager.beforeTestClass().voidbeforeEach(org.junit.jupiter.api.extension.ExtensionContext context) voidbeforeTestExecution(org.junit.jupiter.api.extension.ExtensionContext context) static ApplicationContextgetApplicationContext(org.junit.jupiter.api.extension.ExtensionContext context) Get theApplicationContextassociated with the suppliedExtensionContext.voidpostProcessTestInstance(Object testInstance, org.junit.jupiter.api.extension.ExtensionContext context) Delegates toTestContextManager.prepareTestInstance(java.lang.Object).resolveParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext) Resolve a value for theParameterin the suppliedParameterContextby retrieving the corresponding dependency from the test'sApplicationContext.booleansupportsParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext) Determine if the value for theParameterin the suppliedParameterContextshould be autowired from the test'sApplicationContext.
- 
Constructor Details- 
SpringExtensionpublic SpringExtension()
 
- 
- 
Method Details- 
beforeAllDelegates toTestContextManager.beforeTestClass().- Specified by:
- beforeAllin interface- org.junit.jupiter.api.extension.BeforeAllCallback
- Throws:
- Exception
 
- 
afterAllDelegates toTestContextManager.afterTestClass().- Specified by:
- afterAllin interface- org.junit.jupiter.api.extension.AfterAllCallback
- Throws:
- Exception
 
- 
postProcessTestInstancepublic void postProcessTestInstance(Object testInstance, org.junit.jupiter.api.extension.ExtensionContext context) throws Exception Delegates toTestContextManager.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.- Specified by:
- postProcessTestInstancein interface- org.junit.jupiter.api.extension.TestInstancePostProcessor
- Throws:
- Exception
 
- 
beforeEach- Specified by:
- beforeEachin interface- org.junit.jupiter.api.extension.BeforeEachCallback
- Throws:
- Exception
 
- 
beforeTestExecutionpublic void beforeTestExecution(org.junit.jupiter.api.extension.ExtensionContext context) throws Exception - Specified by:
- beforeTestExecutionin interface- org.junit.jupiter.api.extension.BeforeTestExecutionCallback
- Throws:
- Exception
 
- 
afterTestExecutionpublic void afterTestExecution(org.junit.jupiter.api.extension.ExtensionContext context) throws Exception Delegates toTestContextManager.afterTestExecution(java.lang.Object, java.lang.reflect.Method, java.lang.Throwable).- Specified by:
- afterTestExecutionin interface- org.junit.jupiter.api.extension.AfterTestExecutionCallback
- Throws:
- Exception
 
- 
afterEachDelegates toTestContextManager.afterTestMethod(java.lang.Object, java.lang.reflect.Method, java.lang.Throwable).- Specified by:
- afterEachin interface- org.junit.jupiter.api.extension.AfterEachCallback
- Throws:
- Exception
 
- 
supportsParameterpublic boolean supportsParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.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, PropertyProvider)returnstrue. Note thatisAutowirableConstructor()will be invoked with a fallbackPropertyProviderthat delegates its lookup toExtensionContext.getConfigurationParameter(String).
- The parameter is of type ApplicationContextor a sub-type thereof.
- The parameter is of type ApplicationEventsor a sub-type thereof.
- ParameterResolutionDelegate.isAutowirable(java.lang.reflect.Parameter, int)returns- true.
 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 interface- org.junit.jupiter.api.extension.ParameterResolver
- See Also:
 
- The declaring
 executable is a 
- 
resolveParameter@Nullable public Object resolveParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext) Resolve a value for theParameterin the suppliedParameterContextby retrieving the corresponding dependency from the test'sApplicationContext.- Specified by:
- resolveParameterin interface- org.junit.jupiter.api.extension.ParameterResolver
- See Also:
 
- 
getApplicationContextpublic static ApplicationContext getApplicationContext(org.junit.jupiter.api.extension.ExtensionContext context) Get theApplicationContextassociated with the suppliedExtensionContext.- Parameters:
- context- the current- ExtensionContext(never- null)
- Returns:
- the application context
- Throws:
- IllegalStateException- if an error occurs while retrieving the application context
- See Also:
 
 
-