org.springframework.test.context.web

## Class ServletTestExecutionListener

- java.lang.Object
- - org.springframework.test.context.support.AbstractTestExecutionListener
  - - org.springframework.test.context.web.ServletTestExecutionListener

All Implemented Interfaces:
  TestExecutionListener

    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`.

Since:
  3.2
Author:
  Sam Brannen

### Constructor Summary

Constructors
Constructor and Description
`ServletTestExecutionListener()`

### Method Summary

Methods
`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`.

### Methods inherited from class org.springframework.test.context.support.AbstractTestExecutionListener

`afterTestClass, beforeTestClass`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Constructor Detail

#### ServletTestExecutionListener

    public ServletTestExecutionListener()

### Method Detail

#### prepareTestInstance

    public void prepareTestInstance(TestContext testContext)
                             throws Exception

Sets up thread-local state during the test instance preparation callback phase via Spring Web's `RequestContextHolder`.

Specified by:
  `prepareTestInstance` in interface `TestExecutionListener`
Overrides:
  `prepareTestInstance` in class `AbstractTestExecutionListener`
Parameters:
  `testContext` - the test context for the test; never `null`
Throws:
  `Exception` - allows any exception to propagate
See Also:
  `TestExecutionListener.prepareTestInstance(TestContext)`, `setUpRequestContextIfNecessary(TestContext)`

#### beforeTestMethod

    public void beforeTestMethod(TestContext testContext)
                          throws Exception

Sets up thread-local state before each test method via Spring Web's `RequestContextHolder`.

Specified by:
  `beforeTestMethod` in interface `TestExecutionListener`
Overrides:
  `beforeTestMethod` in class `AbstractTestExecutionListener`
Parameters:
  `testContext` - the test context in which the test method will be executed; never `null`
Throws:
  `Exception` - allows any exception to propagate
See Also:
  `TestExecutionListener.beforeTestMethod(TestContext)`, `setUpRequestContextIfNecessary(TestContext)`

#### afterTestMethod

    public void afterTestMethod(TestContext testContext)
                         throws Exception

Cleans up thread-local state after each test method by resetting Spring Web's `RequestContextHolder`.

Specified by:
  `afterTestMethod` in interface `TestExecutionListener`
Overrides:
  `afterTestMethod` in class `AbstractTestExecutionListener`
Parameters:
  `testContext` - the test context in which the test method was executed; never `null`
Throws:
  `Exception` - allows any exception to propagate
See Also:
  `TestExecutionListener.afterTestMethod(TestContext)`