| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 | = Testing AuthenticationAfter xref:reactive/test/web/setup.adoc[applying the Spring Security support to `WebTestClient`], we can use either annotations or `mutateWith` support -- for example:[tabs]======Java::+[source,java,role="primary"]----import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.mockUser;@Testpublic void messageWhenNotAuthenticated() throws Exception {	this.rest		.get()		.uri("/message")		.exchange()		.expectStatus().isUnauthorized();}// --- WithMockUser ---@Test@WithMockUserpublic void messageWhenWithMockUserThenForbidden() throws Exception {	this.rest		.get()		.uri("/message")		.exchange()		.expectStatus().isEqualTo(HttpStatus.FORBIDDEN);}@Test@WithMockUser(roles = "ADMIN")public void messageWhenWithMockAdminThenOk() throws Exception {	this.rest		.get()		.uri("/message")		.exchange()		.expectStatus().isOk()		.expectBody(String.class).isEqualTo("Hello World!");}// --- mutateWith mockUser ---@Testpublic void messageWhenMutateWithMockUserThenForbidden() throws Exception {	this.rest		.mutateWith(mockUser())		.get()		.uri("/message")		.exchange()		.expectStatus().isEqualTo(HttpStatus.FORBIDDEN);}@Testpublic void messageWhenMutateWithMockAdminThenOk() throws Exception {	this.rest		.mutateWith(mockUser().roles("ADMIN"))		.get()		.uri("/message")		.exchange()		.expectStatus().isOk()		.expectBody(String.class).isEqualTo("Hello World!");}----Kotlin::+[source,kotlin,role="secondary"]----import org.springframework.test.web.reactive.server.expectBodyimport org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.mockUser//...@Test@WithMockUserfun messageWhenWithMockUserThenForbidden() {    this.rest.get().uri("/message")        .exchange()        .expectStatus().isEqualTo(HttpStatus.FORBIDDEN)}@Test@WithMockUser(roles = ["ADMIN"])fun messageWhenWithMockAdminThenOk() {    this.rest.get().uri("/message")        .exchange()        .expectStatus().isOk        .expectBody<String>().isEqualTo("Hello World!")}// --- mutateWith mockUser ---@Testfun messageWhenMutateWithMockUserThenForbidden() {    this.rest        .mutateWith(mockUser())        .get().uri("/message")        .exchange()        .expectStatus().isEqualTo(HttpStatus.FORBIDDEN)}@Testfun messageWhenMutateWithMockAdminThenOk() {    this.rest        .mutateWith(mockUser().roles("ADMIN"))        .get().uri("/message")        .exchange()        .expectStatus().isOk        .expectBody<String>().isEqualTo("Hello World!")}----======In addition to `mockUser()`, Spring Security ships with several other convenience mutators for things like xref:reactive/test/web/csrf.adoc[CSRF] and xref:reactive/test/web/oauth2.adoc[OAuth 2.0].
 |