Class ServletTestExecutionListener
- java.lang.Object
- org.springframework.test.context.support.AbstractTestExecutionListener
- org.springframework.test.context.web.ServletTestExecutionListener
- All Implemented Interfaces:
Ordered
,TestExecutionListener
public class ServletTestExecutionListener extends AbstractTestExecutionListener
TestExecutionListener
which provides mock Servlet API support toWebApplicationContexts
loaded by the Spring TestContext Framework.Specifically,
ServletTestExecutionListener
sets up thread-local state via Spring Web'sRequestContextHolder
during test instance preparation and before each test method and creates aMockHttpServletRequest
,MockHttpServletResponse
, andServletWebRequest
based on theMockServletContext
present in theWebApplicationContext
. This listener also ensures that theMockHttpServletResponse
andServletWebRequest
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
Fields Modifier and Type Field Description static String
ACTIVATE_LISTENER
Attribute name for aTestContext
attribute which indicates that theServletTestExecutionListener
should be activated.static String
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.static String
POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE
Attribute name for aTestContext
attribute which indicates thatServletTestExecutionListener
has already populated Spring Web'sRequestContextHolder
.static String
RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE
Attribute name for aTestContext
attribute which indicates whether or not theServletTestExecutionListener
should reset Spring Web'sRequestContextHolder
inafterTestMethod(TestContext)
.Fields inherited from interface org.springframework.core.Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
Constructor Summary
Constructors Constructor Description ServletTestExecutionListener()
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
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
.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, beforeTestClass
Field Detail
RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE
public static final String RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE
Attribute name for aTestContext
attribute which indicates whether or not theServletTestExecutionListener
should reset Spring Web'sRequestContextHolder
inafterTestMethod(TestContext)
.Permissible values include
Boolean.TRUE
andBoolean.FALSE
.
POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE
public static final String 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
public static final String 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
public static final String 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 Detail
ServletTestExecutionListener
public ServletTestExecutionListener()
Method Detail
getOrder
public final int getOrder()
Returns1000
.- Specified by:
getOrder
in interfaceOrdered
- Overrides:
getOrder
in classAbstractTestExecutionListener
- Returns:
- the order value
- See Also:
Ordered.HIGHEST_PRECEDENCE
,Ordered.LOWEST_PRECEDENCE
prepareTestInstance
public void prepareTestInstance(TestContext testContext) throws Exception
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
public void beforeTestMethod(TestContext testContext) throws Exception
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
public void afterTestMethod(TestContext testContext) throws Exception
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:
TestExecutionListener.afterTestMethod(TestContext)