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 Summary
Constructors -
Method Summary
Modifier 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
-
SpringExtension
public SpringExtension()
-
-
Method Details
-
beforeAll
Delegates toTestContextManager.beforeTestClass().- Specified by:
beforeAllin interfaceorg.junit.jupiter.api.extension.BeforeAllCallback- Throws:
Exception
-
afterAll
Delegates toTestContextManager.afterTestClass().- Specified by:
afterAllin interfaceorg.junit.jupiter.api.extension.AfterAllCallback- Throws:
Exception
-
postProcessTestInstance
public 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 interfaceorg.junit.jupiter.api.extension.TestInstancePostProcessor- Throws:
Exception
-
beforeEach
- Specified by:
beforeEachin interfaceorg.junit.jupiter.api.extension.BeforeEachCallback- Throws:
Exception
-
beforeTestExecution
public void beforeTestExecution(org.junit.jupiter.api.extension.ExtensionContext context) throws Exception - Specified by:
beforeTestExecutionin interfaceorg.junit.jupiter.api.extension.BeforeTestExecutionCallback- Throws:
Exception
-
afterTestExecution
public 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 interfaceorg.junit.jupiter.api.extension.AfterTestExecutionCallback- Throws:
Exception
-
afterEach
Delegates toTestContextManager.afterTestMethod(java.lang.Object, java.lang.reflect.Method, java.lang.Throwable).- Specified by:
afterEachin interfaceorg.junit.jupiter.api.extension.AfterEachCallback- Throws:
Exception
-
supportsParameter
public 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)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 interfaceorg.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 interfaceorg.junit.jupiter.api.extension.ParameterResolver- See Also:
-
getApplicationContext
public static ApplicationContext getApplicationContext(org.junit.jupiter.api.extension.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:
-