Class ServletTestExecutionListener
- All Implemented Interfaces:
Ordered,TestExecutionListener
TestExecutionListener which provides mock Servlet API support to
WebApplicationContexts loaded by the Spring
TestContext Framework.
Specifically, ServletTestExecutionListener sets up thread-local
state via Spring Web's RequestContextHolder during test instance preparation and before each test method and creates a MockHttpServletRequest, MockHttpServletResponse, and
ServletWebRequest based on the MockServletContext present in
the WebApplicationContext. This listener also ensures that the
MockHttpServletResponse and ServletWebRequest can be injected
into the test instance, and once the test is complete this listener cleans up thread-local state.
Note that ServletTestExecutionListener is enabled by default but
generally takes no action if the test
class is not annotated with @WebAppConfiguration.
See the javadocs for individual methods in this class for details.
- Since:
- 3.2
- Author:
- Sam Brannen, Phillip Webb
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringAttribute name for aTestContextattribute which indicates that theServletTestExecutionListenershould be activated.static final StringAttribute name for a request attribute which indicates that theMockHttpServletRequeststored in theRequestAttributesin Spring Web'sRequestContextHolderwas created by the TestContext framework.static final StringAttribute name for aTestContextattribute which indicates thatServletTestExecutionListenerhas already populated Spring Web'sRequestContextHolder.static final StringAttribute name for aTestContextattribute which indicates whether or not theServletTestExecutionListenershould reset Spring Web'sRequestContextHolderinafterTestMethod(TestContext).Fields inherited from interface org.springframework.core.Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidafterTestMethod(TestContext testContext) If theRESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTEin the suppliedTestContexthas a value ofBoolean.TRUE, this method will (1) clean up thread-local state after each test method by resetting Spring Web'sRequestContextHolderand (2) ensure that new mocks are injected into the test instance for subsequent tests by setting theDependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTEin the test context totrue.voidbeforeTestMethod(TestContext testContext) Sets up thread-local state before each test method via Spring Web'sRequestContextHolder, but only if the test class is annotated with@WebAppConfiguration.final intgetOrder()Returns1000.voidprepareTestInstance(TestContext testContext) Sets up thread-local state during the test instance preparation callback phase via Spring Web'sRequestContextHolder, but only if the test class is annotated with@WebAppConfiguration.Methods inherited from class org.springframework.test.context.support.AbstractTestExecutionListener
afterTestClass, afterTestExecution, beforeTestClass, beforeTestExecution
-
Field Details
-
RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE
Attribute name for aTestContextattribute which indicates whether or not theServletTestExecutionListenershould reset Spring Web'sRequestContextHolderinafterTestMethod(TestContext).Permissible values include
Boolean.TRUEandBoolean.FALSE. -
POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE
Attribute name for aTestContextattribute which indicates thatServletTestExecutionListenerhas already populated Spring Web'sRequestContextHolder.Permissible values include
Boolean.TRUEandBoolean.FALSE. -
CREATED_BY_THE_TESTCONTEXT_FRAMEWORK
Attribute name for a request attribute which indicates that theMockHttpServletRequeststored in theRequestAttributesin Spring Web'sRequestContextHolderwas created by the TestContext framework.Permissible values include
Boolean.TRUEandBoolean.FALSE.- Since:
- 4.2
-
ACTIVATE_LISTENER
Attribute name for aTestContextattribute which indicates that theServletTestExecutionListenershould be activated. When not set totrue, activation occurs when the test class is annotated with@WebAppConfiguration.Permissible values include
Boolean.TRUEandBoolean.FALSE.- Since:
- 4.3
-
-
Constructor Details
-
ServletTestExecutionListener
public ServletTestExecutionListener()
-
-
Method Details
-
getOrder
public final int getOrder()Returns1000.- Specified by:
getOrderin interfaceOrdered- Overrides:
getOrderin classAbstractTestExecutionListener- Returns:
- the order value
- See Also:
-
prepareTestInstance
Sets up thread-local state during the test instance preparation callback phase via Spring Web'sRequestContextHolder, but only if the test class is annotated with@WebAppConfiguration.- Specified by:
prepareTestInstancein interfaceTestExecutionListener- Overrides:
prepareTestInstancein classAbstractTestExecutionListener- Parameters:
testContext- the test context for the test; nevernull- Throws:
Exception- allows any exception to propagate- See Also:
-
TestExecutionListener.prepareTestInstance(TestContext)setUpRequestContextIfNecessary(TestContext)
-
beforeTestMethod
Sets up thread-local state before each test method via Spring Web'sRequestContextHolder, but only if the test class is annotated with@WebAppConfiguration.- Specified by:
beforeTestMethodin interfaceTestExecutionListener- Overrides:
beforeTestMethodin classAbstractTestExecutionListener- Parameters:
testContext- the test context in which the test method will be executed; nevernull- Throws:
Exception- allows any exception to propagate- See Also:
-
TestExecutionListener.beforeTestMethod(TestContext)setUpRequestContextIfNecessary(TestContext)
-
afterTestMethod
If theRESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTEin the suppliedTestContexthas a value ofBoolean.TRUE, this method will (1) clean up thread-local state after each test method by resetting Spring Web'sRequestContextHolderand (2) ensure that new mocks are injected into the test instance for subsequent tests by setting theDependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTEin the test context totrue.The
RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTEandPOPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTEwill be subsequently removed from the test context, regardless of their values.- Specified by:
afterTestMethodin interfaceTestExecutionListener- Overrides:
afterTestMethodin classAbstractTestExecutionListener- Parameters:
testContext- the test context in which the test method was executed; nevernull- Throws:
Exception- allows any exception to propagate- See Also:
-