method.adoc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. [[test-method]]
  2. = Testing Method Security
  3. This section demonstrates how to use Spring Security's Test support to test method-based security.
  4. We first introduce a `MessageService` that requires the user to be authenticated to be able to access it:
  5. ====
  6. .Java
  7. [source,java,role="primary"]
  8. ----
  9. public class HelloMessageService implements MessageService {
  10. @PreAuthorize("authenticated")
  11. public String getMessage() {
  12. Authentication authentication = SecurityContextHolder.getContext()
  13. .getAuthentication();
  14. return "Hello " + authentication;
  15. }
  16. }
  17. ----
  18. .Kotlin
  19. [source,kotlin,role="secondary"]
  20. ----
  21. class HelloMessageService : MessageService {
  22. @PreAuthorize("authenticated")
  23. fun getMessage(): String {
  24. val authentication: Authentication = SecurityContextHolder.getContext().authentication
  25. return "Hello $authentication"
  26. }
  27. }
  28. ----
  29. ====
  30. The result of `getMessage` is a `String` that says "`Hello`" to the current Spring Security `Authentication`.
  31. The follwoing listing shows example output:
  32. ====
  33. [source,text]
  34. ----
  35. 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
  36. ----
  37. ====
  38. [[test-method-setup]]
  39. == Security Test Setup
  40. Before we can use the Spring Security test support, we must perform some setup:
  41. ====
  42. .Java
  43. [source,java,role="primary"]
  44. ----
  45. @RunWith(SpringJUnit4ClassRunner.class) // <1>
  46. @ContextConfiguration // <2>
  47. public class WithMockUserTests {
  48. // ...
  49. }
  50. ----
  51. .Kotlin
  52. [source,kotlin,role="secondary"]
  53. ----
  54. @RunWith(SpringJUnit4ClassRunner::class)
  55. @ContextConfiguration
  56. class WithMockUserTests {
  57. // ...
  58. }
  59. ----
  60. <1> `@RunWith` instructs the spring-test module that it should create an `ApplicationContext`. This is no different than using the existing Spring Test support. For additional information, refer to the https://docs.spring.io/spring-framework/docs/4.0.x/spring-framework-reference/htmlsingle/#integration-testing-annotations-standard[Spring Reference]
  61. <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 https://docs.spring.io/spring-framework/docs/4.0.x/spring-framework-reference/htmlsingle/#testcontext-ctx-management[Spring Reference]
  62. ====
  63. [NOTE]
  64. ====
  65. Spring Security hooks into Spring Test support through the `WithSecurityContextTestExecutionListener`, which ensures that our tests are run with the correct user.
  66. It does this by populating the `SecurityContextHolder` prior to running our tests.
  67. If you use reactive method security, you also need `ReactorContextTestExecutionListener`, which populates `ReactiveSecurityContextHolder`.
  68. After the test is done, it clears out the `SecurityContextHolder`.
  69. If you need only Spring Security related support, you can replace `@ContextConfiguration` with `@SecurityTestExecutionListeners`.
  70. ====
  71. Remember, we added the `@PreAuthorize` annotation to our `HelloMessageService`, so it requires an authenticated user to invoke it.
  72. If we run the tests, we expect the following test will pass:
  73. ====
  74. .Java
  75. [source,java,role="primary"]
  76. ----
  77. @Test(expected = AuthenticationCredentialsNotFoundException.class)
  78. public void getMessageUnauthenticated() {
  79. messageService.getMessage();
  80. }
  81. ----
  82. .Kotlin
  83. [source,kotlin,role="secondary"]
  84. ----
  85. @Test(expected = AuthenticationCredentialsNotFoundException::class)
  86. fun getMessageUnauthenticated() {
  87. messageService.getMessage()
  88. }
  89. ----
  90. ====
  91. [[test-method-withmockuser]]
  92. == @WithMockUser
  93. The question is "How could we most easily run the test as a specific user?"
  94. The answer is to use `@WithMockUser`.
  95. The following test will be run as a user with the username "user", the password "password", and the roles "ROLE_USER".
  96. ====
  97. .Java
  98. [source,java,role="primary"]
  99. ----
  100. @Test
  101. @WithMockUser
  102. public void getMessageWithMockUser() {
  103. String message = messageService.getMessage();
  104. ...
  105. }
  106. ----
  107. .Kotlin
  108. [source,kotlin,role="secondary"]
  109. ----
  110. @Test
  111. @WithMockUser
  112. fun getMessageWithMockUser() {
  113. val message: String = messageService.getMessage()
  114. // ...
  115. }
  116. ----
  117. ====
  118. Specifically the following is true:
  119. * The user with a username of `user` does not have to exist, since we mock the user object.
  120. * The `Authentication` that is populated in the `SecurityContext` is of type `UsernamePasswordAuthenticationToken`.
  121. * The principal on the `Authentication` is Spring Security's `User` object.
  122. * The `User` has a username of `user`.
  123. * The `User` has a password of `password`.
  124. * A single `GrantedAuthority` named `ROLE_USER` is used.
  125. The preceding example is handy, because it lets us use a lot of defaults.
  126. What if we wanted to run the test with a different username?
  127. The following test would run with a username of `customUser` (again, the user does not need to actually exist):
  128. ====
  129. .Java
  130. [source,java,role="primary"]
  131. ----
  132. @Test
  133. @WithMockUser("customUsername")
  134. public void getMessageWithMockUserCustomUsername() {
  135. String message = messageService.getMessage();
  136. ...
  137. }
  138. ----
  139. .Kotlin
  140. [source,kotlin,role="secondary"]
  141. ----
  142. @Test
  143. @WithMockUser("customUsername")
  144. fun getMessageWithMockUserCustomUsername() {
  145. val message: String = messageService.getMessage()
  146. // ...
  147. }
  148. ----
  149. ====
  150. We can also easily customize the roles.
  151. For example, the following test is invoked with a username of `admin` and roles of `ROLE_USER` and `ROLE_ADMIN`.
  152. ====
  153. .Java
  154. [source,java,role="primary"]
  155. ----
  156. @Test
  157. @WithMockUser(username="admin",roles={"USER","ADMIN"})
  158. public void getMessageWithMockUserCustomUser() {
  159. String message = messageService.getMessage();
  160. ...
  161. }
  162. ----
  163. .Kotlin
  164. [source,kotlin,role="secondary"]
  165. ----
  166. @Test
  167. @WithMockUser(username="admin",roles=["USER","ADMIN"])
  168. fun getMessageWithMockUserCustomUser() {
  169. val message: String = messageService.getMessage()
  170. // ...
  171. }
  172. ----
  173. ====
  174. If we do not want the value to automatically be prefixed with `ROLE_` we can use the `authorities` attribute.
  175. For example, the following test is invoked with a username of `admin` and the `USER` and `ADMIN` authorities.
  176. ====
  177. .Java
  178. [source,java,role="primary"]
  179. ----
  180. @Test
  181. @WithMockUser(username = "admin", authorities = { "ADMIN", "USER" })
  182. public void getMessageWithMockUserCustomAuthorities() {
  183. String message = messageService.getMessage();
  184. ...
  185. }
  186. ----
  187. .Kotlin
  188. [source,kotlin,role="secondary"]
  189. ----
  190. @Test
  191. @WithMockUser(username = "admin", authorities = ["ADMIN", "USER"])
  192. fun getMessageWithMockUserCustomUsername() {
  193. val message: String = messageService.getMessage()
  194. // ...
  195. }
  196. ----
  197. ====
  198. It can be a bit tedious to place the annotation on every test method.
  199. Instead, we can place the annotation at the class level. Then every test uses the specified user.
  200. 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:
  201. ====
  202. .Java
  203. [source,java,role="primary"]
  204. ----
  205. @RunWith(SpringJUnit4ClassRunner.class)
  206. @ContextConfiguration
  207. @WithMockUser(username="admin",roles={"USER","ADMIN"})
  208. public class WithMockUserTests {
  209. // ...
  210. }
  211. ----
  212. .Kotlin
  213. [source,kotlin,role="secondary"]
  214. ----
  215. @RunWith(SpringJUnit4ClassRunner::class)
  216. @ContextConfiguration
  217. @WithMockUser(username="admin",roles=["USER","ADMIN"])
  218. class WithMockUserTests {
  219. // ...
  220. }
  221. ----
  222. ====
  223. 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.
  224. 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.
  225. ====
  226. .Java
  227. [source,java,role="primary"]
  228. ----
  229. @ExtendWith(SpringExtension.class)
  230. @ContextConfiguration
  231. @WithMockUser(username="admin",roles={"USER","ADMIN"})
  232. public class WithMockUserTests {
  233. @Nested
  234. public class TestSuite1 {
  235. // ... all test methods use admin user
  236. }
  237. @Nested
  238. public class TestSuite2 {
  239. // ... all test methods use admin user
  240. }
  241. }
  242. ----
  243. .Kotlin
  244. [source,kotlin,role="secondary"]
  245. ----
  246. @ExtendWith(SpringExtension::class)
  247. @ContextConfiguration
  248. @WithMockUser(username = "admin", roles = ["USER", "ADMIN"])
  249. class WithMockUserTests {
  250. @Nested
  251. inner class TestSuite1 { // ... all test methods use admin user
  252. }
  253. @Nested
  254. inner class TestSuite2 { // ... all test methods use admin user
  255. }
  256. }
  257. ----
  258. ====
  259. By default, the `SecurityContext` is set during the `TestExecutionListener.beforeTestMethod` event.
  260. This is the equivalent of happening before JUnit's `@Before`.
  261. You can change this to happen during the `TestExecutionListener.beforeTestExecution` event, which is after JUnit's `@Before` but before the test method is invoked:
  262. ====
  263. [source,java]
  264. ----
  265. @WithMockUser(setupBefore = TestExecutionEvent.TEST_EXECUTION)
  266. ----
  267. ====
  268. [[test-method-withanonymoususer]]
  269. == @WithAnonymousUser
  270. Using `@WithAnonymousUser` allows running as an anonymous user.
  271. 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.
  272. The following example runs `withMockUser1` and `withMockUser2` by using <<test-method-withmockuser,@WithMockUser>> and `anonymous` as an anonymous user:
  273. ====
  274. .Java
  275. [source,java,role="primary"]
  276. ----
  277. @RunWith(SpringJUnit4ClassRunner.class)
  278. @WithMockUser
  279. public class WithUserClassLevelAuthenticationTests {
  280. @Test
  281. public void withMockUser1() {
  282. }
  283. @Test
  284. public void withMockUser2() {
  285. }
  286. @Test
  287. @WithAnonymousUser
  288. public void anonymous() throws Exception {
  289. // override default to run as anonymous user
  290. }
  291. }
  292. ----
  293. .Kotlin
  294. [source,kotlin,role="secondary"]
  295. ----
  296. @RunWith(SpringJUnit4ClassRunner::class)
  297. @WithMockUser
  298. class WithUserClassLevelAuthenticationTests {
  299. @Test
  300. fun withMockUser1() {
  301. }
  302. @Test
  303. fun withMockUser2() {
  304. }
  305. @Test
  306. @WithAnonymousUser
  307. fun anonymous() {
  308. // override default to run as anonymous user
  309. }
  310. }
  311. ----
  312. ====
  313. By default, the `SecurityContext` is set during the `TestExecutionListener.beforeTestMethod` event.
  314. This is the equivalent of happening before JUnit's `@Before`.
  315. You can change this to happen during the `TestExecutionListener.beforeTestExecution` event, which is after JUnit's `@Before` but before the test method is invoked:
  316. ====
  317. [source,java]
  318. ----
  319. @WithAnonymousUser(setupBefore = TestExecutionEvent.TEST_EXECUTION)
  320. ----
  321. ====
  322. [[test-method-withuserdetails]]
  323. == @WithUserDetails
  324. While `@WithMockUser` is a convenient way to get started, it may not work in all instances.
  325. For example, some applications expect the `Authentication` principal to be of a specific type.
  326. This is done so that the application can refer to the principal as the custom type and reduce coupling on Spring Security.
  327. The custom principal is often returned by a custom `UserDetailsService` that returns an object that implements both `UserDetails` and the custom type.
  328. For situations like this, it is useful to create the test user by using a custom `UserDetailsService`.
  329. That is exactly what `@WithUserDetails` does.
  330. 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`:
  331. ====
  332. .Java
  333. [source,java,role="primary"]
  334. ----
  335. @Test
  336. @WithUserDetails
  337. public void getMessageWithUserDetails() {
  338. String message = messageService.getMessage();
  339. ...
  340. }
  341. ----
  342. .Kotlin
  343. [source,kotlin,role="secondary"]
  344. ----
  345. @Test
  346. @WithUserDetails
  347. fun getMessageWithUserDetails() {
  348. val message: String = messageService.getMessage()
  349. // ...
  350. }
  351. ----
  352. ====
  353. We can also customize the username used to lookup the user from our `UserDetailsService`.
  354. For example, this test can be run with a principal that is returned from the `UserDetailsService` with the username of `customUsername`:
  355. ====
  356. .Java
  357. [source,java,role="primary"]
  358. ----
  359. @Test
  360. @WithUserDetails("customUsername")
  361. public void getMessageWithUserDetailsCustomUsername() {
  362. String message = messageService.getMessage();
  363. ...
  364. }
  365. ----
  366. .Kotlin
  367. [source,kotlin,role="secondary"]
  368. ----
  369. @Test
  370. @WithUserDetails("customUsername")
  371. fun getMessageWithUserDetailsCustomUsername() {
  372. val message: String = messageService.getMessage()
  373. // ...
  374. }
  375. ----
  376. ====
  377. We can also provide an explicit bean name to look up the `UserDetailsService`.
  378. The following test looks up the username of `customUsername` by using the `UserDetailsService` with a bean name of `myUserDetailsService`:
  379. ====
  380. .Java
  381. [source,java,role="primary"]
  382. ----
  383. @Test
  384. @WithUserDetails(value="customUsername", userDetailsServiceBeanName="myUserDetailsService")
  385. public void getMessageWithUserDetailsServiceBeanName() {
  386. String message = messageService.getMessage();
  387. ...
  388. }
  389. ----
  390. .Kotlin
  391. [source,kotlin,role="secondary"]
  392. ----
  393. @Test
  394. @WithUserDetails(value="customUsername", userDetailsServiceBeanName="myUserDetailsService")
  395. fun getMessageWithUserDetailsServiceBeanName() {
  396. val message: String = messageService.getMessage()
  397. // ...
  398. }
  399. ----
  400. ====
  401. As we did with `@WithMockUser`, we can also place our annotation at the class level so that every test uses the same user.
  402. However, unlike `@WithMockUser`, `@WithUserDetails` requires the user to exist.
  403. By default, the `SecurityContext` is set during the `TestExecutionListener.beforeTestMethod` event.
  404. This is the equivalent of happening before JUnit's `@Before`.
  405. You can change this to happen during the `TestExecutionListener.beforeTestExecution` event, which is after JUnit's `@Before` but before the test method is invoked:
  406. ====
  407. [source,java]
  408. ----
  409. @WithUserDetails(setupBefore = TestExecutionEvent.TEST_EXECUTION)
  410. ----
  411. ====
  412. [[test-method-withsecuritycontext]]
  413. == @WithSecurityContext
  414. We have seen that `@WithMockUser` is an excellent choice if we do not use a custom `Authentication` principal.
  415. Next, we discovered that `@WithUserDetails` lets us use a custom `UserDetailsService` to create our `Authentication` principal but requires the user to exist.
  416. We now see an option that allows the most flexibility.
  417. We can create our own annotation that uses the `@WithSecurityContext` to create any `SecurityContext` we want.
  418. For example, we might create an annotation named `@WithMockCustomUser`:
  419. ====
  420. .Java
  421. [source,java,role="primary"]
  422. ----
  423. @Retention(RetentionPolicy.RUNTIME)
  424. @WithSecurityContext(factory = WithMockCustomUserSecurityContextFactory.class)
  425. public @interface WithMockCustomUser {
  426. String username() default "rob";
  427. String name() default "Rob Winch";
  428. }
  429. ----
  430. .Kotlin
  431. [source,kotlin,role="secondary"]
  432. ----
  433. @Retention(AnnotationRetention.RUNTIME)
  434. @WithSecurityContext(factory = WithMockCustomUserSecurityContextFactory::class)
  435. annotation class WithMockCustomUser(val username: String = "rob", val name: String = "Rob Winch")
  436. ----
  437. ====
  438. You can see that `@WithMockCustomUser` is annotated with the `@WithSecurityContext` annotation.
  439. This is what signals to Spring Security test support that we intend to create a `SecurityContext` for the test.
  440. The `@WithSecurityContext` annotation requires that we specify a `SecurityContextFactory` to create a new `SecurityContext`, given our `@WithMockCustomUser` annotation.
  441. The following listing shows our `WithMockCustomUserSecurityContextFactory` implementation:
  442. ====
  443. .Java
  444. [source,java,role="primary"]
  445. ----
  446. public class WithMockCustomUserSecurityContextFactory
  447. implements WithSecurityContextFactory<WithMockCustomUser> {
  448. @Override
  449. public SecurityContext createSecurityContext(WithMockCustomUser customUser) {
  450. SecurityContext context = SecurityContextHolder.createEmptyContext();
  451. CustomUserDetails principal =
  452. new CustomUserDetails(customUser.name(), customUser.username());
  453. Authentication auth =
  454. new UsernamePasswordAuthenticationToken(principal, "password", principal.getAuthorities());
  455. context.setAuthentication(auth);
  456. return context;
  457. }
  458. }
  459. ----
  460. .Kotlin
  461. [source,kotlin,role="secondary"]
  462. ----
  463. class WithMockCustomUserSecurityContextFactory : WithSecurityContextFactory<WithMockCustomUser> {
  464. override fun createSecurityContext(customUser: WithMockCustomUser): SecurityContext {
  465. val context = SecurityContextHolder.createEmptyContext()
  466. val principal = CustomUserDetails(customUser.name, customUser.username)
  467. val auth: Authentication =
  468. UsernamePasswordAuthenticationToken(principal, "password", principal.authorities)
  469. context.authentication = auth
  470. return context
  471. }
  472. }
  473. ----
  474. ====
  475. 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.
  476. When creating your own `WithSecurityContextFactory` implementations, it is nice to know that they can be annotated with standard Spring annotations.
  477. For example, the `WithUserDetailsSecurityContextFactory` uses the `@Autowired` annotation to acquire the `UserDetailsService`:
  478. ====
  479. .Java
  480. [source,java,role="primary"]
  481. ----
  482. final class WithUserDetailsSecurityContextFactory
  483. implements WithSecurityContextFactory<WithUserDetails> {
  484. private UserDetailsService userDetailsService;
  485. @Autowired
  486. public WithUserDetailsSecurityContextFactory(UserDetailsService userDetailsService) {
  487. this.userDetailsService = userDetailsService;
  488. }
  489. public SecurityContext createSecurityContext(WithUserDetails withUser) {
  490. String username = withUser.value();
  491. Assert.hasLength(username, "value() must be non-empty String");
  492. UserDetails principal = userDetailsService.loadUserByUsername(username);
  493. Authentication authentication = new UsernamePasswordAuthenticationToken(principal, principal.getPassword(), principal.getAuthorities());
  494. SecurityContext context = SecurityContextHolder.createEmptyContext();
  495. context.setAuthentication(authentication);
  496. return context;
  497. }
  498. }
  499. ----
  500. .Kotlin
  501. [source,kotlin,role="secondary"]
  502. ----
  503. class WithUserDetailsSecurityContextFactory @Autowired constructor(private val userDetailsService: UserDetailsService) :
  504. WithSecurityContextFactory<WithUserDetails> {
  505. override fun createSecurityContext(withUser: WithUserDetails): SecurityContext {
  506. val username: String = withUser.value
  507. Assert.hasLength(username, "value() must be non-empty String")
  508. val principal = userDetailsService.loadUserByUsername(username)
  509. val authentication: Authentication =
  510. UsernamePasswordAuthenticationToken(principal, principal.password, principal.authorities)
  511. val context = SecurityContextHolder.createEmptyContext()
  512. context.authentication = authentication
  513. return context
  514. }
  515. }
  516. ----
  517. ====
  518. By default, the `SecurityContext` is set during the `TestExecutionListener.beforeTestMethod` event.
  519. This is the equivalent of happening before JUnit's `@Before`.
  520. You can change this to happen during the `TestExecutionListener.beforeTestExecution` event, which is after JUnit's `@Before` but before the test method is invoked:
  521. ====
  522. [source,java]
  523. ----
  524. @WithSecurityContext(setupBefore = TestExecutionEvent.TEST_EXECUTION)
  525. ----
  526. ====
  527. [[test-method-meta-annotations]]
  528. == Test Meta Annotations
  529. If you reuse the same user within your tests often, it is not ideal to have to repeatedly specify the attributes.
  530. 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:
  531. ====
  532. .Java
  533. [source,java,role="primary"]
  534. ----
  535. @WithMockUser(username="admin",roles={"USER","ADMIN"})
  536. ----
  537. .Kotlin
  538. [source,kotlin,role="secondary"]
  539. ----
  540. @WithMockUser(username="admin",roles=["USER","ADMIN"])
  541. ----
  542. ====
  543. Rather than repeating this everywhere, we can use a meta annotation.
  544. For example, we could create a meta annotation named `WithMockAdmin`:
  545. ====
  546. .Java
  547. [source,java,role="primary"]
  548. ----
  549. @Retention(RetentionPolicy.RUNTIME)
  550. @WithMockUser(value="rob",roles="ADMIN")
  551. public @interface WithMockAdmin { }
  552. ----
  553. .Kotlin
  554. [source,kotlin,role="secondary"]
  555. ----
  556. @Retention(AnnotationRetention.RUNTIME)
  557. @WithMockUser(value = "rob", roles = ["ADMIN"])
  558. annotation class WithMockAdmin
  559. ----
  560. ====
  561. Now we can use `@WithMockAdmin` in the same way as the more verbose `@WithMockUser`.
  562. Meta annotations work with any of the testing annotations described above.
  563. For example, this means we could create a meta annotation for `@WithUserDetails("admin")` as well.