method.adoc 20 KB

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