@Target(value={METHOD,TYPE}) @Retention(value=RUNTIME) @Inherited @Documented @WithSecurityContext(factory=org.springframework.security.test.context.support.WithMockUserSecurityContextFactory.class) public @interface WithMockUser
WithSecurityContextTestExecutionListener
this annotation can be
added to a test method to emulate running with a mocked user. In order to work with
MockMvc
The SecurityContext
that is used will have the following
properties:
SecurityContext
created with be that of
SecurityContextHolder.createEmptyContext()
UsernamePasswordAuthenticationToken
that uses
the username of either value()
or username()
,
GrantedAuthority
that are specified by roles()
, and a password
specified by password()
.
WithUserDetails
Modifier and Type | Optional Element and Description |
---|---|
java.lang.String[] |
authorities
The authorities to use.
|
java.lang.String |
password
The password to be used.
|
java.lang.String[] |
roles
The roles to use.
|
TestExecutionEvent |
setupBefore
Determines when the
SecurityContext is setup. |
java.lang.String |
username
The username to be used.
|
java.lang.String |
value
Convenience mechanism for specifying the username.
|
public abstract java.lang.String value
username()
is specified it will be used instead of value()
public abstract java.lang.String username
value()
is a synonym for
username()
, but if username()
is specified it will take
precedence.public abstract java.lang.String[] roles
The roles to use. The default is "USER". A GrantedAuthority
will be created
for each value within roles. Each value in roles will automatically be prefixed
with "ROLE_". For example, the default will result in "ROLE_USER" being used.
If authorities()
is specified this property cannot be changed from the default.
public abstract java.lang.String password
@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