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 Javadoc for individual methods in this class for details.
| Modifier and Type | Field and Description |
|---|---|
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). |
| 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, beforeTestClasspublic 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 final int getOrder()
1000.getOrder in interface org.springframework.core.OrderedgetOrder in class AbstractTestExecutionListenerpublic void prepareTestInstance(TestContext testContext) throws Exception
RequestContextHolder, but only if
the test class is annotated with
@WebAppConfiguration.prepareTestInstance in interface TestExecutionListenerprepareTestInstance in class AbstractTestExecutionListenertestContext - the test context for the test; never nullException - 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 TestExecutionListenerbeforeTestMethod in class AbstractTestExecutionListenertestContext - the test context in which the test method will be
executed; never nullException - 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 TestExecutionListenerafterTestMethod in class AbstractTestExecutionListenertestContext - the test context in which the test method was
executed; never nullException - allows any exception to propagateTestExecutionListener.afterTestMethod(TestContext)