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
Modifier and TypeFieldDescriptionstatic final String
Attribute name for aTestContext
attribute which indicates that theServletTestExecutionListener
should be activated.static final String
Attribute name for a request attribute which indicates that theMockHttpServletRequest
stored in theRequestAttributes
in Spring Web'sRequestContextHolder
was created by the TestContext framework.static final String
Attribute name for aTestContext
attribute which indicates thatServletTestExecutionListener
has already populated Spring Web'sRequestContextHolder
.static final String
Attribute name for aTestContext
attribute which indicates whether theServletTestExecutionListener
should reset Spring Web'sRequestContextHolder
inafterTestMethod(TestContext)
.Fields inherited from interface org.springframework.core.Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
afterTestMethod
(TestContext testContext) If theRESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE
in the suppliedTestContext
has a value ofBoolean.TRUE
, this method will (1) clean up thread-local state after each test method by resetting Spring Web'sRequestContextHolder
and (2) ensure that new mocks are injected into the test instance for subsequent tests by setting theDependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTE
in the test context totrue
.void
beforeTestMethod
(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 int
getOrder()
Returns1000
.void
prepareTestInstance
(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 aTestContext
attribute which indicates whether theServletTestExecutionListener
should reset Spring Web'sRequestContextHolder
inafterTestMethod(TestContext)
.Permissible values include
Boolean.TRUE
andBoolean.FALSE
. -
POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE
Attribute name for aTestContext
attribute which indicates thatServletTestExecutionListener
has already populated Spring Web'sRequestContextHolder
.Permissible values include
Boolean.TRUE
andBoolean.FALSE
. -
CREATED_BY_THE_TESTCONTEXT_FRAMEWORK
Attribute name for a request attribute which indicates that theMockHttpServletRequest
stored in theRequestAttributes
in Spring Web'sRequestContextHolder
was created by the TestContext framework.Permissible values include
Boolean.TRUE
andBoolean.FALSE
.- Since:
- 4.2
-
ACTIVATE_LISTENER
Attribute name for aTestContext
attribute which indicates that theServletTestExecutionListener
should be activated. When not set totrue
, activation occurs when the test class is annotated with@WebAppConfiguration
.Permissible values include
Boolean.TRUE
andBoolean.FALSE
.- Since:
- 4.3
-
-
Constructor Details
-
ServletTestExecutionListener
public ServletTestExecutionListener()
-
-
Method Details
-
getOrder
public final int getOrder()Returns1000
.- Specified by:
getOrder
in interfaceOrdered
- Overrides:
getOrder
in 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:
prepareTestInstance
in interfaceTestExecutionListener
- Overrides:
prepareTestInstance
in 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:
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- See Also:
-
TestExecutionListener.beforeTestMethod(TestContext)
setUpRequestContextIfNecessary(TestContext)
-
afterTestMethod
If theRESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE
in the suppliedTestContext
has a value ofBoolean.TRUE
, this method will (1) clean up thread-local state after each test method by resetting Spring Web'sRequestContextHolder
and (2) ensure that new mocks are injected into the test instance for subsequent tests by setting theDependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTE
in the test context totrue
.The
RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE
andPOPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE
will be subsequently removed from the test context, regardless of their values.- Specified by:
afterTestMethod
in interfaceTestExecutionListener
- Overrides:
afterTestMethod
in classAbstractTestExecutionListener
- Parameters:
testContext
- the test context in which the test method was executed; nevernull
- Throws:
Exception
- allows any exception to propagate- See Also:
-