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
takes no action if the ApplicationContext
loaded for the current test
is not a WebApplicationContext
.
Constructor and Description |
---|
ServletTestExecutionListener() |
Modifier and Type | Method and Description |
---|---|
void |
afterTestMethod(TestContext testContext)
Cleans up thread-local state after each test method by resetting Spring Web's
RequestContextHolder . |
void |
beforeTestMethod(TestContext testContext)
Sets up thread-local state before each test method via Spring Web's
RequestContextHolder . |
void |
prepareTestInstance(TestContext testContext)
Sets up thread-local state during the test instance preparation
callback phase via Spring Web's
RequestContextHolder . |
afterTestClass, beforeTestClass
public void prepareTestInstance(TestContext testContext) throws Exception
RequestContextHolder
.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
.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
RequestContextHolder
.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)