public class ServletTestExecutionListener extends AbstractTestExecutionListener
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.
Modifier and Type | Field and Description |
---|---|
static String |
ACTIVATE_LISTENER
Attribute name for a
TestContext attribute which indicates that the
ServletTestExecutionListener should be activated. |
static String |
CREATED_BY_THE_TESTCONTEXT_FRAMEWORK
Attribute name for a request attribute which indicates that the
MockHttpServletRequest stored in the RequestAttributes
in Spring Web's RequestContextHolder was created by the TestContext
framework. |
static String |
POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE
Attribute name for a
TestContext attribute which indicates that
ServletTestExecutionListener has already populated Spring Web's
RequestContextHolder . |
static String |
RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE
Attribute name for a
TestContext attribute which indicates
whether or not the ServletTestExecutionListener should reset Spring Web's
RequestContextHolder in afterTestMethod(TestContext) . |
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
Constructor and Description |
---|
ServletTestExecutionListener() |
Modifier and Type | Method and Description |
---|---|
void |
afterTestMethod(TestContext testContext)
If the
RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE in the supplied
TestContext has a value of Boolean.TRUE , this method will
(1) clean up thread-local state after each test method by resetting Spring Web's
RequestContextHolder and (2) ensure that new mocks are injected
into the test instance for subsequent tests by setting the
DependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTE
in the test context to true . |
void |
beforeTestMethod(TestContext testContext)
Sets up thread-local state before each test method via Spring Web's
RequestContextHolder , but only if the
test class is annotated with
@WebAppConfiguration . |
int |
getOrder()
Returns
1000 . |
void |
prepareTestInstance(TestContext testContext)
Sets up thread-local state during the test instance preparation
callback phase via Spring Web's
RequestContextHolder , but only if
the test class is annotated with
@WebAppConfiguration . |
afterTestClass, beforeTestClass
public static final String RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE
TestContext
attribute which indicates
whether or not the ServletTestExecutionListener
should reset Spring Web's
RequestContextHolder
in afterTestMethod(TestContext)
.
Permissible values include Boolean.TRUE
and Boolean.FALSE
.
public static final String POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE
TestContext
attribute which indicates that
ServletTestExecutionListener
has already populated Spring Web's
RequestContextHolder
.
Permissible values include Boolean.TRUE
and Boolean.FALSE
.
public static final String CREATED_BY_THE_TESTCONTEXT_FRAMEWORK
MockHttpServletRequest
stored in the RequestAttributes
in Spring Web's RequestContextHolder
was created by the TestContext
framework.
Permissible values include Boolean.TRUE
and Boolean.FALSE
.
public static final String ACTIVATE_LISTENER
TestContext
attribute which indicates that the
ServletTestExecutionListener
should be activated. When not set to
true
, activation occurs when the test class is annotated with @WebAppConfiguration
.
Permissible values include Boolean.TRUE
and Boolean.FALSE
.
public final int getOrder()
1000
.getOrder
in interface Ordered
getOrder
in class AbstractTestExecutionListener
Ordered.HIGHEST_PRECEDENCE
,
Ordered.LOWEST_PRECEDENCE
public void prepareTestInstance(TestContext testContext) throws Exception
RequestContextHolder
, but only if
the test class is annotated with
@WebAppConfiguration
.prepareTestInstance
in interface TestExecutionListener
prepareTestInstance
in class AbstractTestExecutionListener
testContext
- the test context for the test; never null
Exception
- allows any exception to propagateTestExecutionListener.prepareTestInstance(TestContext)
,
setUpRequestContextIfNecessary(TestContext)
public void beforeTestMethod(TestContext testContext) throws Exception
RequestContextHolder
, but only if the
test class is annotated with
@WebAppConfiguration
.beforeTestMethod
in interface TestExecutionListener
beforeTestMethod
in class AbstractTestExecutionListener
testContext
- the test context in which the test method will be
executed; never null
Exception
- allows any exception to propagateTestExecutionListener.beforeTestMethod(TestContext)
,
setUpRequestContextIfNecessary(TestContext)
public void afterTestMethod(TestContext testContext) throws Exception
RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE
in the supplied
TestContext
has a value of Boolean.TRUE
, this method will
(1) clean up thread-local state after each test method by resetting Spring Web's
RequestContextHolder
and (2) ensure that new mocks are injected
into the test instance for subsequent tests by setting the
DependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTE
in the test context to true
.
The RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE
and
POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE
will be subsequently
removed from the test context, regardless of their values.
afterTestMethod
in interface TestExecutionListener
afterTestMethod
in class AbstractTestExecutionListener
testContext
- the test context in which the test method was
executed; never null
Exception
- allows any exception to propagateTestExecutionListener.afterTestMethod(TestContext)