@Target(value={METHOD,TYPE})
@Retention(value=RUNTIME)
@Inherited
@Documented
@WithSecurityContext(factory=org.springframework.security.test.context.support.WithAnonymousUserSecurityContextFactory.class)
public @interface WithAnonymousUser
WithSecurityContextTestExecutionListener this
annotation can be added to a test method to emulate running with an anonymous
user. The SecurityContext that is used will contain an
AnonymousAuthenticationToken. This is useful when a user wants to run
a majority of tests as a specific user and wishes to override a few methods
to be anonymous. For example:
@WithMockUser
public class SecurityTests {
@Test
@WithAnonymousUser
public void runAsAnonymous() {
// ... run as an anonymous user ...
}
// ... lots of tests ran with a default user ...
}
| Modifier and Type | Optional Element and Description |
|---|---|
TestExecutionEvent |
setupBefore
Determines when the
SecurityContext is setup. |
@AliasFor(annotation=WithSecurityContext.class) public abstract TestExecutionEvent setupBefore
SecurityContext is setup. The default is before
TestExecutionEvent.TEST_METHOD which occurs during
TestExecutionListener.beforeTestMethod(TestContext)TestExecutionEvent to initialize before