Class SpringExtension
java.lang.Object
org.springframework.test.context.junit.jupiter.SpringExtension
- All Implemented Interfaces:
AfterAllCallback,AfterEachCallback,AfterTestExecutionCallback,BeforeAllCallback,BeforeEachCallback,BeforeTestExecutionCallback,Extension,ParameterResolver,TestInstancePostProcessor,TestInstantiationAwareExtension
public class SpringExtension
extends Object
implements BeforeAllCallback, AfterAllCallback, TestInstancePostProcessor, BeforeEachCallback, AfterEachCallback, BeforeTestExecutionCallback, AfterTestExecutionCallback, ParameterResolver
SpringExtension integrates the Spring TestContext Framework
into the JUnit Jupiter testing framework.
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, Simon Baslé
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.junit.jupiter.api.extension.TestInstantiationAwareExtension
TestInstantiationAwareExtension.ExtensionContextScope -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidafterAll(ExtensionContext context) Delegates toTestContextManager.afterTestClass().voidafterEach(ExtensionContext context) voidafterTestExecution(ExtensionContext context) voidbeforeAll(ExtensionContext context) Delegates toTestContextManager.beforeTestClass().voidbeforeEach(ExtensionContext context) voidbeforeTestExecution(ExtensionContext context) static ApplicationContextgetApplicationContext(ExtensionContext context) Get theApplicationContextassociated with the suppliedExtensionContext.voidpostProcessTestInstance(Object testInstance, ExtensionContext context) Delegates toTestContextManager.prepareTestInstance(java.lang.Object).resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) Resolve a value for theParameterin the suppliedParameterContextby retrieving the corresponding dependency from the test'sApplicationContext.booleansupportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) Determine if the value for theParameterin the suppliedParameterContextshould be autowired from the test'sApplicationContext.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.junit.jupiter.api.extension.TestInstantiationAwareExtension
getTestInstantiationExtensionContextScope
-
Constructor Details
-
SpringExtension
public SpringExtension()
-
-
Method Details
-
beforeAll
Delegates toTestContextManager.beforeTestClass().- Specified by:
beforeAllin interfaceBeforeAllCallback- Throws:
Exception
-
afterAll
Delegates toTestContextManager.afterTestClass().- Specified by:
afterAllin interfaceAfterAllCallback- Throws:
Exception
-
postProcessTestInstance
Delegates toTestContextManager.prepareTestInstance(java.lang.Object).This method also validates that test methods and test lifecycle methods are not annotated with
@Autowired.- Specified by:
postProcessTestInstancein interfaceTestInstancePostProcessor- Throws:
Exception
-
beforeEach
- Specified by:
beforeEachin interfaceBeforeEachCallback- Throws:
Exception
-
beforeTestExecution
- Specified by:
beforeTestExecutionin interfaceBeforeTestExecutionCallback- Throws:
Exception
-
afterTestExecution
Delegates toTestContextManager.afterTestExecution(java.lang.Object, java.lang.reflect.Method, java.lang.Throwable).- Specified by:
afterTestExecutionin interfaceAfterTestExecutionCallback- Throws:
Exception
-
afterEach
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, 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 interfaceParameterResolver- See Also:
- The declaring
executable is a
-
resolveParameter
public @Nullable 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:
-
getApplicationContext
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:
-