Annotation Type WithAnonymousUser
-
@Target({METHOD,TYPE}) @Retention(RUNTIME) @Inherited @Documented @WithSecurityContext(factory=org.springframework.security.test.context.support.WithAnonymousUserSecurityContextFactory.class) public @interface WithAnonymousUser
When used withWithSecurityContextTestExecutionListener
this annotation can be added to a test method to emulate running with an anonymous user. TheSecurityContext
that is used will contain anAnonymousAuthenticationToken
. 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 ... }
- Since:
- 4.1
-
-
Optional Element Summary
Optional Elements Modifier and Type Optional Element Description TestExecutionEvent
setupBefore
Determines when theSecurityContext
is setup.
-
-
-
Element Detail
-
setupBefore
@AliasFor(annotation=WithSecurityContext.class) TestExecutionEvent setupBefore
Determines when theSecurityContext
is setup. The default is beforeTestExecutionEvent.TEST_METHOD
which occurs duringTestExecutionListener.beforeTestMethod(TestContext)
- Returns:
- the
TestExecutionEvent
to initialize before - Since:
- 5.1
- Default:
- org.springframework.security.test.context.support.TestExecutionEvent.TEST_METHOD
-
-