Class WithSecurityContextTestExecutionListener
java.lang.Object
org.springframework.test.context.support.AbstractTestExecutionListener
org.springframework.security.test.context.support.WithSecurityContextTestExecutionListener
- All Implemented Interfaces:
org.springframework.core.Ordered
,org.springframework.test.context.TestExecutionListener
public class WithSecurityContextTestExecutionListener
extends org.springframework.test.context.support.AbstractTestExecutionListener
A
TestExecutionListener
that will find annotations that are annotated with
WithSecurityContext
on a test method or at the class level. If found, the
WithSecurityContext.factory()
is used to create a SecurityContext
that
will be used with this test. If using with MockMvc
the
SecurityMockMvcRequestPostProcessors.testSecurityContext()
needs to be used
too.- Since:
- 4.0
- See Also:
-
Field Summary
Fields inherited from interface org.springframework.core.Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
afterTestMethod
(org.springframework.test.context.TestContext testContext) Clears out theTestSecurityContextHolder
and theSecurityContextHolder
after each test method.void
beforeTestExecution
(org.springframework.test.context.TestContext testContext) If configured before test execution sets the SecurityContextvoid
beforeTestMethod
(org.springframework.test.context.TestContext testContext) Sets up theSecurityContext
for each test method.int
getOrder()
Returns10000
.Methods inherited from class org.springframework.test.context.support.AbstractTestExecutionListener
afterTestClass, afterTestExecution, beforeTestClass, prepareTestInstance
-
Constructor Details
-
WithSecurityContextTestExecutionListener
public WithSecurityContextTestExecutionListener()
-
-
Method Details
-
beforeTestMethod
public void beforeTestMethod(org.springframework.test.context.TestContext testContext) Sets up theSecurityContext
for each test method. First the specific method is inspected for aWithSecurityContext
orAnnotation
that hasWithSecurityContext
on it. If that is not found, the class is inspected. If still not found, then noSecurityContext
is populated.- Specified by:
beforeTestMethod
in interfaceorg.springframework.test.context.TestExecutionListener
- Overrides:
beforeTestMethod
in classorg.springframework.test.context.support.AbstractTestExecutionListener
-
beforeTestExecution
public void beforeTestExecution(org.springframework.test.context.TestContext testContext) If configured before test execution sets the SecurityContext- Specified by:
beforeTestExecution
in interfaceorg.springframework.test.context.TestExecutionListener
- Overrides:
beforeTestExecution
in classorg.springframework.test.context.support.AbstractTestExecutionListener
- Since:
- 5.1
-
afterTestMethod
public void afterTestMethod(org.springframework.test.context.TestContext testContext) Clears out theTestSecurityContextHolder
and theSecurityContextHolder
after each test method.- Specified by:
afterTestMethod
in interfaceorg.springframework.test.context.TestExecutionListener
- Overrides:
afterTestMethod
in classorg.springframework.test.context.support.AbstractTestExecutionListener
-
getOrder
public int getOrder()Returns10000
.- Specified by:
getOrder
in interfaceorg.springframework.core.Ordered
- Overrides:
getOrder
in classorg.springframework.test.context.support.AbstractTestExecutionListener
-