2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								[[test-method]]
							 
						 
					
						
							
								
									
										
										
										
											2021-07-30 13:52:15 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								= Testing Method Security
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-04-21 16:01:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								This section demonstrates how to use Spring Security's Test support to test method-based security.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								We first introduce a `MessageService` that requires the user to be authenticated to be able to access it:
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[tabs]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Java::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-06-24 11:49:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,java,role="primary"]
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								public class HelloMessageService implements MessageService {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									@PreAuthorize("authenticated")
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									public String getMessage() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										Authentication authentication = SecurityContextHolder.getContext()
							 
						 
					
						
							
								
									
										
										
										
											2018-09-11 21:08:22 +09:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											.getAuthentication();
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										return "Hello " + authentication;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Kotlin::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-06-24 11:49:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,kotlin,role="secondary"]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								class HelloMessageService : MessageService {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    @PreAuthorize("authenticated")
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    fun getMessage(): String {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        val authentication: Authentication = SecurityContextHolder.getContext().authentication
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        return "Hello $authentication"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
									
										
										
										
											2021-06-24 11:49:13 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-04-21 16:01:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The result of `getMessage` is a `String` that says "`Hello`" to the current Spring Security `Authentication`.
							 
						 
					
						
							
								
									
										
										
										
											2023-11-11 16:05:31 -03:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The following listing shows example output:
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								[source,text]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Hello org.springframework.security.authentication.UsernamePasswordAuthenticationToken@ca25360: Principal: org.springframework.security.core.userdetails.User@36ebcb: Username: user; Password: [PROTECTED]; Enabled: true; AccountNonExpired: true; credentialsNonExpired: true; AccountNonLocked: true; Granted Authorities: ROLE_USER; Credentials: [PROTECTED]; Authenticated: true; Details: null; Granted Authorities: ROLE_USER
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								[[test-method-setup]]
							 
						 
					
						
							
								
									
										
										
										
											2021-07-30 13:52:15 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								== Security Test Setup
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-04-21 16:01:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Before we can use the Spring Security test support, we must perform some setup:
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[tabs]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Java::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-06-16 10:31:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,java,role="primary"]
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
									
										
										
										
											2022-03-22 20:30:10 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								@ExtendWith(SpringExtension.class) // <1>
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								@ContextConfiguration // <2>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								public class WithMockUserTests {
							 
						 
					
						
							
								
									
										
										
										
											2021-12-13 16:57:36 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// ...
							 
						 
					
						
							
								
									
										
										
										
											2021-04-21 16:01:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Kotlin::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-06-16 10:31:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,kotlin,role="secondary"]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
									
										
										
										
											2022-03-22 20:30:10 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								@ExtendWith(SpringExtension.class)
							 
						 
					
						
							
								
									
										
										
										
											2021-06-16 10:31:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								@ContextConfiguration
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								class WithMockUserTests {
							 
						 
					
						
							
								
									
										
										
										
											2021-12-13 16:57:36 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    // ...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
									
										
										
										
											2021-06-16 10:31:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
									
										
										
										
											2022-05-27 12:42:28 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								<1> `@ExtendWith` instructs the spring-test module that it should create an `ApplicationContext`. For additional information, refer to the {spring-framework-reference-url}testing.html#testcontext-junit-jupiter-extension[Spring reference].
							 
						 
					
						
							
								
									
										
										
										
											2022-05-27 13:00:20 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								<2> `@ContextConfiguration` instructs the spring-test the configuration to use to create the `ApplicationContext`. Since no configuration is specified, the default configuration locations will be tried. This is no different than using the existing Spring Test support. For additional information, refer to the {spring-framework-reference-url}testing.html#spring-testing-annotation-contextconfiguration[Spring Reference].
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-06-04 14:12:59 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[NOTE]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								====
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Spring Security hooks into Spring Test support through the `WithSecurityContextTestExecutionListener`, which ensures that our tests are run with the correct user.
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								It does this by populating the `SecurityContextHolder` prior to running our tests.
							 
						 
					
						
							
								
									
										
										
										
											2021-04-21 16:01:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								If you use reactive method security, you also need `ReactorContextTestExecutionListener`, which populates `ReactiveSecurityContextHolder`.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								After the test is done, it clears out the `SecurityContextHolder`.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								If you need only Spring Security related support, you can replace `@ContextConfiguration` with `@SecurityTestExecutionListeners`.
							 
						 
					
						
							
								
									
										
										
										
											2021-06-04 14:12:59 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								====
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-04-21 16:01:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Remember, we added the `@PreAuthorize` annotation to our `HelloMessageService`, so it requires an authenticated user to invoke it.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								If we run the tests, we expect the following test will pass:
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[tabs]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Java::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-06-16 10:31:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,java,role="primary"]
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@Test(expected = AuthenticationCredentialsNotFoundException.class)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								public void getMessageUnauthenticated() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									messageService.getMessage();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Kotlin::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-06-16 10:31:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,kotlin,role="secondary"]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@Test(expected = AuthenticationCredentialsNotFoundException::class)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								fun getMessageUnauthenticated() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    messageService.getMessage()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
									
										
										
										
											2021-06-16 10:31:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								[[test-method-withmockuser]]
							 
						 
					
						
							
								
									
										
										
										
											2021-07-30 13:52:15 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								== @WithMockUser
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The question is "How could we most easily run the test as a specific user?"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The answer is to use `@WithMockUser`.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The following test will be run as a user with the username "user", the password "password", and the roles "ROLE_USER".
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[tabs]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Java::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-06-16 10:31:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,java,role="primary"]
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@Test
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@WithMockUser
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								public void getMessageWithMockUser() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								String message = messageService.getMessage();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Kotlin::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-06-16 10:31:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,kotlin,role="secondary"]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@Test
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@WithMockUser
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								fun getMessageWithMockUser() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    val message: String = messageService.getMessage()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // ...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
									
										
										
										
											2021-06-16 10:31:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								Specifically the following is true:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-04-21 16:01:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								* The user with a username of `user` does not have to exist, since we mock the user object.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								* The `Authentication` that is populated in the `SecurityContext` is of type `UsernamePasswordAuthenticationToken`.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								* The principal on the `Authentication` is Spring Security's `User` object.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								* The `User` has a username of `user`.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								* The `User` has a password of `password`.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								* A single `GrantedAuthority` named `ROLE_USER` is used.
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-04-21 16:01:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The preceding example is handy, because it lets us use a lot of defaults.
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								What if we wanted to run the test with a different username?
							 
						 
					
						
							
								
									
										
										
										
											2021-04-21 16:01:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The following test would run with a username of `customUser` (again, the user does not need to actually exist):
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[tabs]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Java::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-06-16 10:31:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,java,role="primary"]
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@Test
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@WithMockUser("customUsername")
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								public void getMessageWithMockUserCustomUsername() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									String message = messageService.getMessage();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Kotlin::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-06-16 10:31:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,kotlin,role="secondary"]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@Test
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@WithMockUser("customUsername")
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								fun getMessageWithMockUserCustomUsername() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    val message: String = messageService.getMessage()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // ...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
									
										
										
										
											2021-06-16 10:31:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								We can also easily customize the roles.
							 
						 
					
						
							
								
									
										
										
										
											2021-04-21 16:01:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								For example, the following test is invoked with a username of `admin` and roles of `ROLE_USER` and `ROLE_ADMIN`.
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[tabs]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Java::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-06-16 10:31:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,java,role="primary"]
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@Test
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@WithMockUser(username="admin",roles={"USER","ADMIN"})
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								public void getMessageWithMockUserCustomUser() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									String message = messageService.getMessage();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Kotlin::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-06-16 10:31:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,kotlin,role="secondary"]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@Test
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@WithMockUser(username="admin",roles=["USER","ADMIN"])
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								fun getMessageWithMockUserCustomUser() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    val message: String = messageService.getMessage()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // ...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
									
										
										
										
											2021-06-16 10:31:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-04-21 16:01:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								If we do not want the value to automatically be prefixed with `ROLE_` we can use the `authorities` attribute.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								For example, the following test is invoked with a username of `admin` and the `USER` and `ADMIN` authorities.
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[tabs]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Java::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-06-16 10:31:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,java,role="primary"]
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@Test
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@WithMockUser(username = "admin", authorities = { "ADMIN", "USER" })
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								public void getMessageWithMockUserCustomAuthorities() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									String message = messageService.getMessage();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Kotlin::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-06-16 10:31:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,kotlin,role="secondary"]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@Test
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@WithMockUser(username = "admin", authorities = ["ADMIN", "USER"])
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								fun getMessageWithMockUserCustomUsername() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    val message: String = messageService.getMessage()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // ...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
									
										
										
										
											2021-06-16 10:31:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-04-21 16:01:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								It can be a bit tedious to place the annotation on every test method.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Instead, we can place the annotation at the class level. Then every test uses the specified user.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The following example runs every test with a user whose username is `admin`, whose password is `password`, and who has the `ROLE_USER` and `ROLE_ADMIN` roles:
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[tabs]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Java::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-06-16 10:31:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,java,role="primary"]
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
									
										
										
										
											2022-03-22 20:30:10 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								@ExtendWith(SpringExtension.class)
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								@ContextConfiguration
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@WithMockUser(username="admin",roles={"USER","ADMIN"})
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								public class WithMockUserTests {
							 
						 
					
						
							
								
									
										
										
										
											2021-12-13 16:57:36 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// ...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Kotlin::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-06-16 10:31:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,kotlin,role="secondary"]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
									
										
										
										
											2022-03-22 20:30:10 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								@ExtendWith(SpringExtension.class)
							 
						 
					
						
							
								
									
										
										
										
											2021-06-16 10:31:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								@ContextConfiguration
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@WithMockUser(username="admin",roles=["USER","ADMIN"])
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								class WithMockUserTests {
							 
						 
					
						
							
								
									
										
										
										
											2021-12-13 16:57:36 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    // ...
							 
						 
					
						
							
								
									
										
										
										
											2021-04-21 16:01:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
									
										
										
										
											2021-06-16 10:31:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
									
										
										
										
											2021-06-16 10:31:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-04-21 16:01:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								If you use JUnit 5's `@Nested` test support, you can also place the annotation on the enclosing class to apply to all nested classes.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The following example runs every test with a user whose username is `admin`, whose password is `password`, and who has the `ROLE_USER` and `ROLE_ADMIN` roles for both test methods.
							 
						 
					
						
							
								
									
										
										
										
											2020-11-03 17:53:59 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[tabs]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Java::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-06-16 10:31:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,java,role="primary"]
							 
						 
					
						
							
								
									
										
										
										
											2020-11-03 17:53:59 -06:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@ExtendWith(SpringExtension.class)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@ContextConfiguration
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@WithMockUser(username="admin",roles={"USER","ADMIN"})
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								public class WithMockUserTests {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									@Nested
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									public class TestSuite1 {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										// ... all test methods use admin user
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									@Nested
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									public class TestSuite2 {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										// ... all test methods use admin user
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Kotlin::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-06-16 10:31:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,kotlin,role="secondary"]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@ExtendWith(SpringExtension::class)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@ContextConfiguration
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@WithMockUser(username = "admin", roles = ["USER", "ADMIN"])
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								class WithMockUserTests {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    @Nested
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    inner class TestSuite1 { // ... all test methods use admin user
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    @Nested
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    inner class TestSuite2 { // ... all test methods use admin user
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
									
										
										
										
											2021-06-16 10:31:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-04-21 16:01:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								By default, the `SecurityContext` is set during the `TestExecutionListener.beforeTestMethod` event.
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								This is the equivalent of happening before JUnit's `@Before`.
							 
						 
					
						
							
								
									
										
										
										
											2021-04-21 16:01:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								You can change this to happen during the `TestExecutionListener.beforeTestExecution` event, which is after JUnit's `@Before` but before the test method is invoked:
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								[source,java]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@WithMockUser(setupBefore = TestExecutionEvent.TEST_EXECUTION)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								[[test-method-withanonymoususer]]
							 
						 
					
						
							
								
									
										
										
										
											2021-07-30 13:52:15 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								== @WithAnonymousUser
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Using `@WithAnonymousUser` allows running as an anonymous user.
							 
						 
					
						
							
								
									
										
										
										
											2021-04-21 16:01:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								This is especially convenient when you wish to run most of your tests with a specific user but want to run a few tests as an anonymous user.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The following example runs `withMockUser1` and `withMockUser2` by using <<test-method-withmockuser,@WithMockUser>> and `anonymous` as an anonymous user:
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[tabs]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Java::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-06-16 10:31:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,java,role="primary"]
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
									
										
										
										
											2022-03-22 20:30:10 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								@ExtendWith(SpringExtension.class)
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								@WithMockUser
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								public class WithUserClassLevelAuthenticationTests {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									@Test
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									public void withMockUser1() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									@Test
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									public void withMockUser2() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									@Test
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									@WithAnonymousUser
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									public void anonymous() throws Exception {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										// override default to run as anonymous user
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Kotlin::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-06-16 10:31:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,kotlin,role="secondary"]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
									
										
										
										
											2022-03-22 20:30:10 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								@ExtendWith(SpringExtension.class)
							 
						 
					
						
							
								
									
										
										
										
											2021-06-16 10:31:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								@WithMockUser
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								class WithUserClassLevelAuthenticationTests {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    @Test
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    fun withMockUser1() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    @Test
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    fun withMockUser2() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    @Test
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    @WithAnonymousUser
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    fun anonymous() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        // override default to run as anonymous user
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
									
										
										
										
											2021-06-16 10:31:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-04-21 16:01:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								By default, the `SecurityContext` is set during the `TestExecutionListener.beforeTestMethod` event.
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								This is the equivalent of happening before JUnit's `@Before`.
							 
						 
					
						
							
								
									
										
										
										
											2021-04-21 16:01:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								You can change this to happen during the `TestExecutionListener.beforeTestExecution` event, which is after JUnit's `@Before` but before the test method is invoked:
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								[source,java]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@WithAnonymousUser(setupBefore = TestExecutionEvent.TEST_EXECUTION)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								[[test-method-withuserdetails]]
							 
						 
					
						
							
								
									
										
										
										
											2021-07-30 13:52:15 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								== @WithUserDetails
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-04-21 16:01:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								While `@WithMockUser` is a convenient way to get started, it may not work in all instances.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								For example, some applications expect the `Authentication` principal to be of a specific type.
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								This is done so that the application can refer to the principal as the custom type and reduce coupling on Spring Security.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-04-21 16:01:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The custom principal is often returned by a custom `UserDetailsService` that returns an object that implements both `UserDetails` and the custom type.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								For situations like this, it is useful to create the test user by using a custom `UserDetailsService`.
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								That is exactly what `@WithUserDetails` does.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-04-21 16:01:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Assuming we have a `UserDetailsService` exposed as a bean, the following test is invoked with an `Authentication` of type `UsernamePasswordAuthenticationToken` and a principal that is returned from the `UserDetailsService` with the username of `user`:
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[tabs]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Java::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-06-16 10:31:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,java,role="primary"]
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@Test
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@WithUserDetails
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								public void getMessageWithUserDetails() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									String message = messageService.getMessage();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Kotlin::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-06-16 10:31:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,kotlin,role="secondary"]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@Test
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@WithUserDetails
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								fun getMessageWithUserDetails() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    val message: String = messageService.getMessage()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // ...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
									
										
										
										
											2021-06-16 10:31:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								We can also customize the username used to lookup the user from our `UserDetailsService`.
							 
						 
					
						
							
								
									
										
										
										
											2021-04-21 16:01:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								For example, this test can be run with a principal that is returned from the `UserDetailsService` with the username of `customUsername`:
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[tabs]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Java::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-06-16 10:31:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,java,role="primary"]
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@Test
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@WithUserDetails("customUsername")
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								public void getMessageWithUserDetailsCustomUsername() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									String message = messageService.getMessage();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Kotlin::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-06-16 10:31:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,kotlin,role="secondary"]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@Test
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@WithUserDetails("customUsername")
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								fun getMessageWithUserDetailsCustomUsername() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    val message: String = messageService.getMessage()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // ...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
									
										
										
										
											2021-06-16 10:31:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								We can also provide an explicit bean name to look up the `UserDetailsService`.
							 
						 
					
						
							
								
									
										
										
										
											2021-04-21 16:01:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The following test looks up the username of `customUsername` by using the `UserDetailsService` with a bean name of `myUserDetailsService`:
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[tabs]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Java::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-06-16 10:31:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,java,role="primary"]
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@Test
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@WithUserDetails(value="customUsername", userDetailsServiceBeanName="myUserDetailsService")
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								public void getMessageWithUserDetailsServiceBeanName() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									String message = messageService.getMessage();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Kotlin::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-06-16 10:31:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,kotlin,role="secondary"]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@Test
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@WithUserDetails(value="customUsername", userDetailsServiceBeanName="myUserDetailsService")
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								fun getMessageWithUserDetailsServiceBeanName() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    val message: String = messageService.getMessage()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // ...
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
									
										
										
										
											2021-06-16 10:31:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-04-21 16:01:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								As we did with `@WithMockUser`, we can also place our annotation at the class level so that every test uses the same user.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								However, unlike `@WithMockUser`, `@WithUserDetails` requires the user to exist.
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-04-21 16:01:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								By default, the `SecurityContext` is set during the `TestExecutionListener.beforeTestMethod` event.
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								This is the equivalent of happening before JUnit's `@Before`.
							 
						 
					
						
							
								
									
										
										
										
											2021-04-21 16:01:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								You can change this to happen during the `TestExecutionListener.beforeTestExecution` event, which is after JUnit's `@Before` but before the test method is invoked:
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								[source,java]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@WithUserDetails(setupBefore = TestExecutionEvent.TEST_EXECUTION)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								[[test-method-withsecuritycontext]]
							 
						 
					
						
							
								
									
										
										
										
											2021-07-30 13:52:15 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								== @WithSecurityContext
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-04-21 16:01:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								We have seen that `@WithMockUser` is an excellent choice if we do not use a custom `Authentication` principal.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Next, we discovered that `@WithUserDetails` lets us use a custom `UserDetailsService` to create our `Authentication` principal but requires the user to exist.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								We now see an option that allows the most flexibility.
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								We can create our own annotation that uses the `@WithSecurityContext` to create any `SecurityContext` we want.
							 
						 
					
						
							
								
									
										
										
										
											2021-04-21 16:01:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								For example, we might create an annotation named `@WithMockCustomUser`:
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[tabs]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Java::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-06-16 10:31:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,java,role="primary"]
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@Retention(RetentionPolicy.RUNTIME)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@WithSecurityContext(factory = WithMockCustomUserSecurityContextFactory.class)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								public @interface WithMockCustomUser {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									String username() default "rob";
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									String name() default "Rob Winch";
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Kotlin::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-06-16 10:31:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,kotlin,role="secondary"]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@Retention(AnnotationRetention.RUNTIME)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@WithSecurityContext(factory = WithMockCustomUserSecurityContextFactory::class)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								annotation class WithMockCustomUser(val username: String = "rob", val name: String = "Rob Winch")
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
									
										
										
										
											2021-06-16 10:31:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								You can see that `@WithMockCustomUser` is annotated with the `@WithSecurityContext` annotation.
							 
						 
					
						
							
								
									
										
										
										
											2021-04-21 16:01:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								This is what signals to Spring Security test support that we intend to create a `SecurityContext` for the test.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The `@WithSecurityContext` annotation requires that we specify a `SecurityContextFactory` to create a new `SecurityContext`, given our `@WithMockCustomUser` annotation.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The following listing shows our `WithMockCustomUserSecurityContextFactory` implementation:
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[tabs]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Java::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-06-16 10:31:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,java,role="primary"]
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								public class WithMockCustomUserSecurityContextFactory
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									implements WithSecurityContextFactory<WithMockCustomUser> {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									@Override
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									public SecurityContext createSecurityContext(WithMockCustomUser customUser) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										SecurityContext context = SecurityContextHolder.createEmptyContext();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										CustomUserDetails principal =
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											new CustomUserDetails(customUser.name(), customUser.username());
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										Authentication auth =
							 
						 
					
						
							
								
									
										
										
										
											2022-03-08 11:33:13 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											UsernamePasswordAuthenticationToken.authenticated(principal, "password", principal.getAuthorities());
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										context.setAuthentication(auth);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										return context;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Kotlin::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-06-16 10:31:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,kotlin,role="secondary"]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								class WithMockCustomUserSecurityContextFactory : WithSecurityContextFactory<WithMockCustomUser> {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    override fun createSecurityContext(customUser: WithMockCustomUser): SecurityContext {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        val context = SecurityContextHolder.createEmptyContext()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        val principal = CustomUserDetails(customUser.name, customUser.username)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        val auth: Authentication =
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            UsernamePasswordAuthenticationToken(principal, "password", principal.authorities)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        context.authentication = auth
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        return context
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
									
										
										
										
											2021-06-16 10:31:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-04-21 16:01:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								We can now annotate a test class or a test method with our new annotation and Spring Security's `WithSecurityContextTestExecutionListener` to ensure that our `SecurityContext` is populated appropriately.
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								When creating your own `WithSecurityContextFactory` implementations, it is nice to know that they can be annotated with standard Spring annotations.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								For example, the `WithUserDetailsSecurityContextFactory` uses the `@Autowired` annotation to acquire the `UserDetailsService`:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[tabs]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Java::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-06-16 10:31:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,java,role="primary"]
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								final class WithUserDetailsSecurityContextFactory
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									implements WithSecurityContextFactory<WithUserDetails> {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									private UserDetailsService userDetailsService;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									@Autowired
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									public WithUserDetailsSecurityContextFactory(UserDetailsService userDetailsService) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										this.userDetailsService = userDetailsService;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									public SecurityContext createSecurityContext(WithUserDetails withUser) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										String username = withUser.value();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										Assert.hasLength(username, "value() must be non-empty String");
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										UserDetails principal = userDetailsService.loadUserByUsername(username);
							 
						 
					
						
							
								
									
										
										
										
											2022-03-08 11:33:13 +01:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										Authentication authentication = UsernamePasswordAuthenticationToken.authenticated(principal, principal.getPassword(), principal.getAuthorities());
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										SecurityContext context = SecurityContextHolder.createEmptyContext();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										context.setAuthentication(authentication);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										return context;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Kotlin::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-06-16 10:31:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,kotlin,role="secondary"]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								class WithUserDetailsSecurityContextFactory @Autowired constructor(private val userDetailsService: UserDetailsService) :
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    WithSecurityContextFactory<WithUserDetails> {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    override fun createSecurityContext(withUser: WithUserDetails): SecurityContext {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        val username: String = withUser.value
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        Assert.hasLength(username, "value() must be non-empty String")
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        val principal = userDetailsService.loadUserByUsername(username)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        val authentication: Authentication =
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            UsernamePasswordAuthenticationToken(principal, principal.password, principal.authorities)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        val context = SecurityContextHolder.createEmptyContext()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        context.authentication = authentication
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        return context
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
									
										
										
										
											2021-06-16 10:31:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-04-21 16:01:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								By default, the `SecurityContext` is set during the `TestExecutionListener.beforeTestMethod` event.
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								This is the equivalent of happening before JUnit's `@Before`.
							 
						 
					
						
							
								
									
										
										
										
											2021-04-21 16:01:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								You can change this to happen during the `TestExecutionListener.beforeTestExecution` event, which is after JUnit's `@Before` but before the test method is invoked:
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								[source,java]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@WithSecurityContext(setupBefore = TestExecutionEvent.TEST_EXECUTION)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								[[test-method-meta-annotations]]
							 
						 
					
						
							
								
									
										
										
										
											2021-07-30 13:52:15 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								== Test Meta Annotations
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								If you reuse the same user within your tests often, it is not ideal to have to repeatedly specify the attributes.
							 
						 
					
						
							
								
									
										
										
										
											2021-04-21 16:01:26 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								For example, if you have many tests related to an administrative user with a username of `admin` and roles of `ROLE_USER` and `ROLE_ADMIN`, you have to write:
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[tabs]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Java::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-06-16 10:31:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,java,role="primary"]
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@WithMockUser(username="admin",roles={"USER","ADMIN"})
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Kotlin::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-06-16 10:31:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,kotlin,role="secondary"]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@WithMockUser(username="admin",roles=["USER","ADMIN"])
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
									
										
										
										
											2021-06-16 10:31:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								Rather than repeating this everywhere, we can use a meta annotation.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								For example, we could create a meta annotation named `WithMockAdmin`:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[tabs]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Java::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-06-16 10:31:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,java,role="primary"]
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@Retention(RetentionPolicy.RUNTIME)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@WithMockUser(value="rob",roles="ADMIN")
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								public @interface WithMockAdmin { }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Kotlin::
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								+
							 
						 
					
						
							
								
									
										
										
										
											2021-06-16 10:31:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								[source,kotlin,role="secondary"]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@Retention(AnnotationRetention.RUNTIME)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								@WithMockUser(value = "rob", roles = ["ADMIN"])
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								annotation class WithMockAdmin
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
									
										
										
										
											2023-06-18 21:30:41 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								======
							 
						 
					
						
							
								
									
										
										
										
											2021-06-16 10:31:29 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-06 09:24:13 -06:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								Now we can use `@WithMockAdmin` in the same way as the more verbose `@WithMockUser`.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Meta annotations work with any of the testing annotations described above.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								For example, this means we could create a meta annotation for `@WithUserDetails("admin")` as well.