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 729d0a1d13..b18320ce0d 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 @@ -395,9 +395,9 @@ class DefaultWebTestClient implements WebTestClient { } @Override - public T consumeWith(Consumer consumer) { + public T consumeWith(Consumer> consumer) { B actual = this.result.getResponseBody(); - this.result.assertWithDiagnostics(() -> consumer.accept(actual)); + this.result.assertWithDiagnostics(() -> consumer.accept(this.result)); return self(); } @@ -493,13 +493,7 @@ class DefaultWebTestClient implements WebTestClient { @Override public JsonPathAssertions jsonPath(String expression, Object... args) { - return new JsonPathAssertions(this, expression, args); - } - - @Override - public BodyContentSpec consumeAsStringWith(Consumer consumer) { - this.result.assertWithDiagnostics(() -> consumer.accept(getBodyAsString())); - return this; + return new JsonPathAssertions(this, getBodyAsString(), expression, args); } private String getBodyAsString() { @@ -512,8 +506,8 @@ class DefaultWebTestClient implements WebTestClient { } @Override - public BodyContentSpec consumeWith(Consumer consumer) { - this.result.assertWithDiagnostics(() -> consumer.accept(this.result.getResponseBody())); + public BodyContentSpec consumeWith(Consumer> consumer) { + this.result.assertWithDiagnostics(() -> consumer.accept(this.result)); return this; } diff --git a/spring-test/src/main/java/org/springframework/test/web/reactive/server/JsonPathAssertions.java b/spring-test/src/main/java/org/springframework/test/web/reactive/server/JsonPathAssertions.java index 30d9b69de8..b4eaa2e97e 100644 --- a/spring-test/src/main/java/org/springframework/test/web/reactive/server/JsonPathAssertions.java +++ b/spring-test/src/main/java/org/springframework/test/web/reactive/server/JsonPathAssertions.java @@ -29,11 +29,14 @@ public class JsonPathAssertions { private final WebTestClient.BodyContentSpec bodySpec; + private final String content; + private final JsonPathExpectationsHelper pathHelper; - JsonPathAssertions(WebTestClient.BodyContentSpec spec, String expression, Object... args) { + JsonPathAssertions(WebTestClient.BodyContentSpec spec, String content, String expression, Object... args) { this.bodySpec = spec; + this.content = content; this.pathHelper = new JsonPathExpectationsHelper(expression, args); } @@ -42,9 +45,7 @@ public class JsonPathAssertions { * Applies {@link JsonPathExpectationsHelper#assertValue(String, Object)}. */ public WebTestClient.BodyContentSpec isEqualTo(Object expectedValue) { - this.bodySpec.consumeAsStringWith(body -> { - this.pathHelper.assertValue(body, expectedValue); - }); + this.pathHelper.assertValue(this.content, expectedValue); return this.bodySpec; } @@ -52,7 +53,7 @@ public class JsonPathAssertions { * Applies {@link JsonPathExpectationsHelper#exists(String)}. */ public WebTestClient.BodyContentSpec exists() { - this.bodySpec.consumeAsStringWith(this.pathHelper::exists); + this.pathHelper.exists(this.content); return this.bodySpec; } @@ -60,7 +61,7 @@ public class JsonPathAssertions { * Applies {@link JsonPathExpectationsHelper#doesNotExist(String)}. */ public WebTestClient.BodyContentSpec doesNotExist() { - this.bodySpec.consumeAsStringWith(this.pathHelper::doesNotExist); + this.pathHelper.doesNotExist(this.content); return this.bodySpec; } @@ -68,7 +69,7 @@ public class JsonPathAssertions { * Applies {@link JsonPathExpectationsHelper#assertValueIsEmpty(String)}. */ public WebTestClient.BodyContentSpec isEmpty() { - this.bodySpec.consumeAsStringWith(this.pathHelper::assertValueIsEmpty); + this.pathHelper.assertValueIsEmpty(this.content); return this.bodySpec; } @@ -76,7 +77,7 @@ public class JsonPathAssertions { * Applies {@link JsonPathExpectationsHelper#assertValueIsNotEmpty(String)}. */ public WebTestClient.BodyContentSpec isNotEmpty() { - this.bodySpec.consumeAsStringWith(this.pathHelper::assertValueIsNotEmpty); + this.pathHelper.assertValueIsNotEmpty(this.content); return this.bodySpec; } @@ -84,7 +85,7 @@ public class JsonPathAssertions { * Applies {@link JsonPathExpectationsHelper#assertValueIsBoolean(String)}. */ public WebTestClient.BodyContentSpec isBoolean() { - this.bodySpec.consumeAsStringWith(this.pathHelper::assertValueIsBoolean); + this.pathHelper.assertValueIsBoolean(this.content); return this.bodySpec; } @@ -92,7 +93,7 @@ public class JsonPathAssertions { * Applies {@link JsonPathExpectationsHelper#assertValueIsNumber(String)}. */ public WebTestClient.BodyContentSpec isNumber() { - this.bodySpec.consumeAsStringWith(this.pathHelper::assertValueIsNumber); + this.pathHelper.assertValueIsNumber(this.content); return this.bodySpec; } @@ -100,7 +101,7 @@ public class JsonPathAssertions { * Applies {@link JsonPathExpectationsHelper#assertValueIsArray(String)}. */ public WebTestClient.BodyContentSpec isArray() { - this.bodySpec.consumeAsStringWith(this.pathHelper::assertValueIsArray); + this.pathHelper.assertValueIsArray(this.content); return this.bodySpec; } @@ -108,7 +109,7 @@ public class JsonPathAssertions { * Applies {@link JsonPathExpectationsHelper#assertValueIsMap(String)}. */ public WebTestClient.BodyContentSpec isMap() { - this.bodySpec.consumeAsStringWith(this.pathHelper::assertValueIsMap); + this.pathHelper.assertValueIsMap(this.content); return this.bodySpec; } 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 cb4f223a2e..009eae64f8 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 @@ -579,9 +579,9 @@ public interface WebTestClient { T isEqualTo(B expected); /** - * Assert the extracted body with the given {@link Consumer}. + * Assert the exchange result with the given {@link Consumer}. */ - T consumeWith(Consumer consumer); + T consumeWith(Consumer> consumer); /** * Return the exchange result with the decoded body. @@ -648,21 +648,12 @@ public interface WebTestClient { */ JsonPathAssertions jsonPath(String expression, Object... args); - /** - * Assert the response body content converted to a String with the given - * {@link Consumer}. The String is created with the {@link Charset} from - * the "content-type" response header or {@code UTF-8} otherwise. - * @param consumer the consumer for the response body; the input String - * may be {@code null} if there was no response body. - */ - BodyContentSpec consumeAsStringWith(Consumer consumer); - /** * Assert the response body content with the given {@link Consumer}. * @param consumer the consumer for the response body; the input * {@code byte[]} may be {@code null} if there was no response body. */ - BodyContentSpec consumeWith(Consumer consumer); + BodyContentSpec consumeWith(Consumer> consumer); /** * Return the exchange result with body content as {@code byte[]}. diff --git a/spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/ResponseEntityTests.java b/spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/ResponseEntityTests.java index 7100e5d93a..775abfdcab 100644 --- a/spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/ResponseEntityTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/ResponseEntityTests.java @@ -70,7 +70,8 @@ public class ResponseEntityTests { .exchange() .expectStatus().isOk() .expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8) - .expectBody(Person.class).consumeWith(p -> assertEquals(new Person("John"), p)); + .expectBody(Person.class) + .consumeWith(result -> assertEquals(new Person("John"), result.getResponseBody())); } @Test diff --git a/spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/bind/ApplicationContextTests.java b/spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/bind/ApplicationContextTests.java index 51b1089ca4..239bdc7999 100644 --- a/spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/bind/ApplicationContextTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/bind/ApplicationContextTests.java @@ -83,7 +83,8 @@ public class ApplicationContextTests { this.client.get().uri("/principal") .exchange() .expectStatus().isOk() - .expectBody().consumeAsStringWith(body -> assertEquals("Hello Mr. Pablo!", body)); + .expectBody(String.class) + .consumeWith(result -> assertEquals("Hello Mr. Pablo!", result.getResponseBody())); } @Test diff --git a/spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/bind/ControllerTests.java b/spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/bind/ControllerTests.java index 8bf27695b7..43ba532bc8 100644 --- a/spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/bind/ControllerTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/bind/ControllerTests.java @@ -74,7 +74,8 @@ public class ControllerTests { this.client.get().uri("/principal") .exchange() .expectStatus().isOk() - .expectBody().consumeAsStringWith(body -> assertEquals("Hello Mr. Pablo!", body)); + .expectBody(String.class) + .consumeWith(result -> assertEquals("Hello Mr. Pablo!", result.getResponseBody())); } @Test