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
-
Method Summary
Modifier and TypeMethodDescriptionvoid
afterAll
(org.junit.jupiter.api.extension.ExtensionContext context) Delegates toTestContextManager.afterTestClass()
.void
afterEach
(org.junit.jupiter.api.extension.ExtensionContext context) void
afterTestExecution
(org.junit.jupiter.api.extension.ExtensionContext context) void
beforeAll
(org.junit.jupiter.api.extension.ExtensionContext context) Delegates toTestContextManager.beforeTestClass()
.void
beforeEach
(org.junit.jupiter.api.extension.ExtensionContext context) void
beforeTestExecution
(org.junit.jupiter.api.extension.ExtensionContext context) static ApplicationContext
getApplicationContext
(org.junit.jupiter.api.extension.ExtensionContext context) Get theApplicationContext
associated with the suppliedExtensionContext
.void
postProcessTestInstance
(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 theParameter
in the suppliedParameterContext
by retrieving the corresponding dependency from the test'sApplicationContext
.boolean
supportsParameter
(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext) Determine if the value for theParameter
in the suppliedParameterContext
should be autowired from the test'sApplicationContext
.
-
Constructor Details
-
SpringExtension
public SpringExtension()
-
-
Method Details
-
beforeAll
Delegates toTestContextManager.beforeTestClass()
.- Specified by:
beforeAll
in interfaceorg.junit.jupiter.api.extension.BeforeAllCallback
- Throws:
Exception
-
afterAll
Delegates toTestContextManager.afterTestClass()
.- Specified by:
afterAll
in 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:
postProcessTestInstance
in interfaceorg.junit.jupiter.api.extension.TestInstancePostProcessor
- Throws:
Exception
-
beforeEach
- Specified by:
beforeEach
in interfaceorg.junit.jupiter.api.extension.BeforeEachCallback
- Throws:
Exception
-
beforeTestExecution
public void beforeTestExecution(org.junit.jupiter.api.extension.ExtensionContext context) throws Exception - Specified by:
beforeTestExecution
in 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:
afterTestExecution
in interfaceorg.junit.jupiter.api.extension.AfterTestExecutionCallback
- Throws:
Exception
-
afterEach
Delegates toTestContextManager.afterTestMethod(java.lang.Object, java.lang.reflect.Method, java.lang.Throwable)
.- Specified by:
afterEach
in 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 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, PropertyProvider)
returnstrue
. Note thatisAutowirableConstructor()
will be invoked with a fallbackPropertyProvider
that delegates its lookup toExtensionContext.getConfigurationParameter(String)
. - The parameter is of type
ApplicationContext
or a sub-type thereof. - The parameter is of type
ApplicationEvents
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 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 theParameter
in the suppliedParameterContext
by retrieving the corresponding dependency from the test'sApplicationContext
.- Specified by:
resolveParameter
in interfaceorg.junit.jupiter.api.extension.ParameterResolver
- See Also:
-
getApplicationContext
public static ApplicationContext getApplicationContext(org.junit.jupiter.api.extension.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:
-