Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,12 @@
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.test.json.AbstractJsonContentAssert;
import org.springframework.test.json.JsonAssert;
import org.springframework.test.json.JsonComparator;
import org.springframework.test.json.JsonCompareMode;
import org.springframework.test.json.JsonContent;
import org.springframework.test.json.JsonContentAssert;
import org.springframework.test.util.AssertionErrors;
import org.springframework.test.util.ExceptionCollector;
import org.springframework.test.util.XmlExpectationsHelper;
Expand Down Expand Up @@ -490,6 +493,15 @@ public BodyContentSpec consumeWith(Consumer<EntityExchangeResult<byte[]>> consum
return this;
}

@Override
public BodyContentSpec consumeWithJsonAssert(Consumer<AbstractJsonContentAssert<?>> jsonAssertConsumer) {
this.result.assertWithDiagnostics(() -> {
JsonContentAssert jsonContentAssert = new JsonContent(getBodyAsString()).assertThat();
jsonAssertConsumer.accept(jsonContentAssert);
});
return this;
}

@Override
public EntityExchangeResult<byte[]> returnResult() {
return this.result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.converter.HttpMessageConverters;
import org.springframework.test.json.AbstractJsonContentAssert;
import org.springframework.test.json.JsonComparator;
import org.springframework.test.json.JsonCompareMode;
import org.springframework.test.json.JsonComparison;
Expand Down Expand Up @@ -810,6 +811,27 @@ default XpathAssertions xpath(String expression, Object... args) {
*/
BodyContentSpec consumeWith(Consumer<EntityExchangeResult<byte[]>> consumer);

/**
* Assert the response body content with the given {@link Consumer}.
* This consumer provides a {@link AbstractJsonContentAssert} that supports
* making fine-grained assertions about the JSON response body, e.g.
* <pre>
* {@code
* restTestClient.get().uri("/test")
* .exchangeSuccessfully()
* .expectBody()
* .consumeWithJsonAssert(jsonContentAssert -> {
* jsonContentAssert.extractingPath("$.users")
* .asArray().singleElement()
* .extracting("id", "name", "email")
* .containsExactly(123, "John Doe", "[email protected]");
* });
* }
* </pre>
* @param jsonAssertConsumer the consumer for the response body
*/
BodyContentSpec consumeWithJsonAssert(Consumer<AbstractJsonContentAssert<?>> jsonAssertConsumer);

/**
* Exit the chained API and return an {@code ExchangeResult} with the
* raw response content.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,19 @@ void testExpectCookie() {
.exchange()
.expectCookie().value("session", Matchers.equalTo("abc"));
}

@Test
void testConsumeWithJsonAssert() {
var bodyContentSpec = RestTestClientTests.this.client.get().uri("/test")
.exchangeSuccessfully()
.expectBody()
.consumeWithJsonAssert(jsonContentAssert -> jsonContentAssert
.hasPath("method")
.hasPathSatisfying("uri", uri -> assertThat(uri).isEqualTo("/test"))
.extractingPath("headers").asMap().hasSize(2)
);
assertThat(bodyContentSpec).isNotNull();
}
}


Expand Down