Class DependencyInjectionTestExecutionListener
- java.lang.Object
- org.springframework.test.context.support.AbstractTestExecutionListener
- org.springframework.test.context.support.DependencyInjectionTestExecutionListener
- All Implemented Interfaces:
Ordered
,TestExecutionListener
public class DependencyInjectionTestExecutionListener extends AbstractTestExecutionListener
TestExecutionListener
which provides support for dependency injection and initialization of test instances.- Since:
- 2.5
- Author:
- Sam Brannen, Juergen Hoeller
Field Summary
Fields Modifier and Type Field Description static String
REINJECT_DEPENDENCIES_ATTRIBUTE
Attribute name for aTestContext
attribute which indicates whether or not the dependencies of a test instance should be reinjected inbeforeTestMethod()
.Fields inherited from interface org.springframework.core.Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
Constructor Summary
Constructors Constructor Description DependencyInjectionTestExecutionListener()
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
beforeTestMethod(TestContext testContext)
If theREINJECT_DEPENDENCIES_ATTRIBUTE
in the suppliedtest context
has a value ofBoolean.TRUE
, this method will have the same effect asprepareTestInstance()
; otherwise, this method will have no effect.int
getOrder()
Returns2000
.protected void
injectDependencies(TestContext testContext)
Performs dependency injection and bean initialization for the suppliedTestContext
as described inprepareTestInstance()
.void
prepareTestInstance(TestContext testContext)
Performs dependency injection on thetest instance
of the suppliedtest context
byautowiring
andinitializing
the test instance via its ownapplication context
(without checking dependencies).Methods inherited from class org.springframework.test.context.support.AbstractTestExecutionListener
afterTestClass, afterTestMethod, beforeTestClass
Field Detail
REINJECT_DEPENDENCIES_ATTRIBUTE
public static final String REINJECT_DEPENDENCIES_ATTRIBUTE
Attribute name for aTestContext
attribute which indicates whether or not the dependencies of a test instance should be reinjected inbeforeTestMethod()
. Note that dependencies will be injected inprepareTestInstance()
in any case.Clients of a
TestContext
(e.g., otherTestExecutionListeners
) may therefore choose to set this attribute to signal that dependencies should be reinjected between execution of individual test methods.Permissible values include
Boolean.TRUE
andBoolean.FALSE
.
Constructor Detail
DependencyInjectionTestExecutionListener
public DependencyInjectionTestExecutionListener()
Method Detail
getOrder
public final int getOrder()
Returns2000
.- Specified by:
getOrder
in interfaceOrdered
- Overrides:
getOrder
in classAbstractTestExecutionListener
- Returns:
- the order value
- See Also:
Ordered.HIGHEST_PRECEDENCE
,Ordered.LOWEST_PRECEDENCE
prepareTestInstance
public void prepareTestInstance(TestContext testContext) throws Exception
Performs dependency injection on thetest instance
of the suppliedtest context
byautowiring
andinitializing
the test instance via its ownapplication context
(without checking dependencies).The
REINJECT_DEPENDENCIES_ATTRIBUTE
will be subsequently removed from the test context, regardless of its value.- Specified by:
prepareTestInstance
in interfaceTestExecutionListener
- Overrides:
prepareTestInstance
in classAbstractTestExecutionListener
- Parameters:
testContext
- the test context for the test; nevernull
- Throws:
Exception
- allows any exception to propagate
beforeTestMethod
public void beforeTestMethod(TestContext testContext) throws Exception
If theREINJECT_DEPENDENCIES_ATTRIBUTE
in the suppliedtest context
has a value ofBoolean.TRUE
, this method will have the same effect asprepareTestInstance()
; otherwise, this method will have no effect.- Specified by:
beforeTestMethod
in interfaceTestExecutionListener
- Overrides:
beforeTestMethod
in classAbstractTestExecutionListener
- Parameters:
testContext
- the test context in which the test method will be executed; nevernull
- Throws:
Exception
- allows any exception to propagate
injectDependencies
protected void injectDependencies(TestContext testContext) throws Exception
Performs dependency injection and bean initialization for the suppliedTestContext
as described inprepareTestInstance()
.The
REINJECT_DEPENDENCIES_ATTRIBUTE
will be subsequently removed from the test context, regardless of its value.- Parameters:
testContext
- the test context for which dependency injection should be performed (nevernull
)- Throws:
Exception
- allows any exception to propagate- See Also:
prepareTestInstance(TestContext)
,beforeTestMethod(TestContext)