From f5c54f90b309957b16ebebdeba0d57e8e59d7f37 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Fri, 14 Jul 2017 16:24:15 +0200 Subject: [PATCH] Add shortcut to mutate + apply to WebTestClient Issue: SPR-15770 --- .../web/reactive/server/DefaultWebTestClient.java | 5 +++++ .../test/web/reactive/server/WebTestClient.java | 13 ++++++++++++- .../server/samples/ExchangeMutatorTests.java | 11 ++++------- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClient.java b/spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClient.java index 081afad4ee..30759c0926 100644 --- a/spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClient.java +++ b/spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClient.java @@ -141,6 +141,11 @@ class DefaultWebTestClient implements WebTestClient { return this.builder; } + @Override + public WebTestClient mutateWith(WebTestClientConfigurer configurer) { + return mutate().apply(configurer).build(); + } + private class DefaultRequestBodyUriSpec implements RequestBodyUriSpec { diff --git a/spring-test/src/main/java/org/springframework/test/web/reactive/server/WebTestClient.java b/spring-test/src/main/java/org/springframework/test/web/reactive/server/WebTestClient.java index 373f5eaa82..2126d10a94 100644 --- a/spring-test/src/main/java/org/springframework/test/web/reactive/server/WebTestClient.java +++ b/spring-test/src/main/java/org/springframework/test/web/reactive/server/WebTestClient.java @@ -135,12 +135,23 @@ public interface WebTestClient { RequestBodyUriSpec method(HttpMethod method); - /** * Return a builder to mutate properties of this web test client. */ Builder mutate(); + /** + * Mutate the {@link WebTestClient}, apply the given configurer, and build + * a new instance. Essentially a shortcut for: + *
+	 * mutate().apply(configurer).build();
+	 * 
+ * @param configurer the configurer to apply + * @return the mutated test client + */ + WebTestClient mutateWith(WebTestClientConfigurer configurer); + + // Static, factory methods /** diff --git a/spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/ExchangeMutatorTests.java b/spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/ExchangeMutatorTests.java index 12f310497f..685e686199 100644 --- a/spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/ExchangeMutatorTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/ExchangeMutatorTests.java @@ -47,7 +47,7 @@ public class ExchangeMutatorTests { public void setUp() throws Exception { this.webTestClient = WebTestClient.bindToController(new TestController()) - .apply(globalIdentity("Pablo")) + .apply(identity("Pablo")) .build(); } @@ -62,7 +62,8 @@ public class ExchangeMutatorTests { @Test public void useLocallyConfiguredIdentity() throws Exception { - withIdentity(this.webTestClient, "Giovanni") + this.webTestClient + .mutateWith(identity("Giovanni")) .get().uri("/userIdentity") .exchange() .expectStatus().isOk() @@ -70,14 +71,10 @@ public class ExchangeMutatorTests { } - private static MockServerConfigurer globalIdentity(String userName) { + private static IdentityConfigurer identity(String userName) { return new IdentityConfigurer(userName); } - private static WebTestClient withIdentity(WebTestClient client, String userName) { - return client.mutate().apply(new IdentityConfigurer(userName)).build(); - } - @RestController static class TestController {