Annotation Interface WithMockUser
@Target({METHOD,TYPE})
@Retention(RUNTIME)
@Inherited
@Documented
@WithSecurityContext(factory=org.springframework.security.test.context.support.WithMockUserSecurityContextFactory.class)
public @interface WithMockUser
When used with
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:
- The
SecurityContextcreated with be that ofSecurityContextHolder.createEmptyContext() - It will be populated with an
UsernamePasswordAuthenticationTokenthat uses the username of eithervalue()orusername(),GrantedAuthoritythat are specified byroles(), and a password specified bypassword().
- Since:
- 4.0
- See Also:
-
Optional Element Summary
Optional ElementsModifier and TypeOptional ElementDescriptionString[]The authorities to use.The password to be used.String[]The roles to use.Determines when theSecurityContextis setup.The username to be used.Convenience mechanism for specifying the username.
-
Element Details
-
value
String valueConvenience mechanism for specifying the username. The default is "user". Ifusername()is specified it will be used instead ofvalue()- Returns:
- Default:
- "user"
-
username
String usernameThe username to be used. Note thatvalue()is a synonym forusername(), but ifusername()is specified it will take precedence.- Returns:
- Default:
- ""
-
roles
String[] rolesThe roles to use. The default is "USER". A
GrantedAuthoritywill 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.- Returns:
- Default:
- {"USER"}
-
authorities
String[] authoritiesThe authorities to use. A
GrantedAuthoritywill be created for each value.If this property is specified then
roles()is not used. This differs fromroles()in that it does not prefix the values passed in automatically.- Returns:
- Default:
- {}
-
password
String passwordThe password to be used. The default is "password".- Returns:
- Default:
- "password"
-
setupBefore
Determines when theSecurityContextis setup. The default is beforeTestExecutionEvent.TEST_METHODwhich occurs duringTestExecutionListener.beforeTestMethod(TestContext)- Returns:
- the
TestExecutionEventto initialize before - Since:
- 5.1
- Default:
- TEST_METHOD
-