类 ServletTestExecutionListener
- java.lang.Object
- org.springframework.test.context.support.AbstractTestExecutionListener
- org.springframework.test.context.web.ServletTestExecutionListener
- 所有已实现的接口:
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.- 从以下版本开始:
- 3.2
- 作者:
- Sam Brannen, Phillip Webb
字段概要
字段 修饰符和类型 字段 说明 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)
.从接口继承的字段 org.springframework.core.Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
构造器概要
构造器 构造器 说明 ServletTestExecutionListener()
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 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
.从类继承的方法 org.springframework.test.context.support.AbstractTestExecutionListener
afterTestClass, beforeTestClass
字段详细资料
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
.- 从以下版本开始:
- 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
.- 从以下版本开始:
- 4.3
构造器详细资料
ServletTestExecutionListener
public ServletTestExecutionListener()
方法详细资料
getOrder
public final int getOrder()
Returns1000
.- 指定者:
getOrder
在接口中Ordered
- 覆盖:
getOrder
在类中AbstractTestExecutionListener
- 返回:
- the order value
- 另请参阅:
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
.- 指定者:
prepareTestInstance
在接口中TestExecutionListener
- 覆盖:
prepareTestInstance
在类中AbstractTestExecutionListener
- 参数:
testContext
- the test context for the test; nevernull
- 抛出:
Exception
- allows any exception to propagate- 另请参阅:
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
.- 指定者:
beforeTestMethod
在接口中TestExecutionListener
- 覆盖:
beforeTestMethod
在类中AbstractTestExecutionListener
- 参数:
testContext
- the test context in which the test method will be executed; nevernull
- 抛出:
Exception
- allows any exception to propagate- 另请参阅:
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.- 指定者:
afterTestMethod
在接口中TestExecutionListener
- 覆盖:
afterTestMethod
在类中AbstractTestExecutionListener
- 参数:
testContext
- the test context in which the test method was executed; nevernull
- 抛出:
Exception
- allows any exception to propagate- 另请参阅:
TestExecutionListener.afterTestMethod(TestContext)