method.adoc 20 KB

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