Skip to content

Commit d8804c7

Browse files
committed
RestTestClient correctly exposes the response body
Closes gh-35385
1 parent 55c315e commit d8804c7

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
lines changed

spring-test/src/main/java/org/springframework/test/web/servlet/client/DefaultRestTestClient.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -286,33 +286,33 @@ public CookieAssertions expectCookie() {
286286

287287
@Override
288288
public <B> BodySpec<B, ?> expectBody(Class<B> bodyType) {
289-
B body = this.exchangeResult.getBody(bodyType);
289+
B body = this.exchangeResult.getClientResponse().bodyTo(bodyType);
290290
EntityExchangeResult<B> result = new EntityExchangeResult<>(this.exchangeResult, body);
291291
return new DefaultBodySpec<>(result);
292292
}
293293

294294
@Override
295295
public <B> BodySpec<B, ?> expectBody(ParameterizedTypeReference<B> bodyType) {
296-
B body = this.exchangeResult.getBody(bodyType);
296+
B body = this.exchangeResult.getClientResponse().bodyTo(bodyType);
297297
EntityExchangeResult<B> result = initExchangeResult(body);
298298
return new DefaultBodySpec<>(result);
299299
}
300300

301301
@Override
302302
public BodyContentSpec expectBody() {
303-
byte[] body = this.exchangeResult.getBody(byte[].class);
303+
byte[] body = this.exchangeResult.getClientResponse().bodyTo(byte[].class);
304304
EntityExchangeResult<byte[]> result = initExchangeResult(body);
305305
return new DefaultBodyContentSpec(result);
306306
}
307307

308308
@Override
309309
public <T> EntityExchangeResult<T> returnResult(Class<T> elementClass) {
310-
return initExchangeResult(this.exchangeResult.getBody(elementClass));
310+
return initExchangeResult(this.exchangeResult.getClientResponse().bodyTo(elementClass));
311311
}
312312

313313
@Override
314314
public <T> EntityExchangeResult<T> returnResult(ParameterizedTypeReference<T> elementTypeRef) {
315-
return initExchangeResult(this.exchangeResult.getBody(elementTypeRef));
315+
return initExchangeResult(this.exchangeResult.getClientResponse().bodyTo(elementTypeRef));
316316
}
317317

318318
private <B> EntityExchangeResult<B> initExchangeResult(@Nullable B body) {
@@ -412,7 +412,7 @@ private static class DefaultBodyContentSpec implements BodyContentSpec {
412412
@Override
413413
public EntityExchangeResult<Void> isEmpty() {
414414
this.result.assertWithDiagnostics(() ->
415-
AssertionErrors.assertTrue("Expected empty body", this.result.getBody(byte[].class) == null));
415+
AssertionErrors.assertTrue("Expected empty body", this.result.getResponseBody() == null));
416416
return new EntityExchangeResult<>(this.result, null);
417417
}
418418

spring-test/src/main/java/org/springframework/test/web/servlet/client/ExchangeResult.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import org.apache.commons.logging.LogFactory;
3030
import org.jspecify.annotations.Nullable;
3131

32-
import org.springframework.core.ParameterizedTypeReference;
3332
import org.springframework.http.HttpHeaders;
3433
import org.springframework.http.HttpMethod;
3534
import org.springframework.http.HttpRequest;
@@ -160,14 +159,11 @@ private static ResponseCookie toResponseCookie(HttpCookie cookie, @Nullable Stri
160159
.build();
161160
}
162161

163-
@Nullable
164-
public <T> T getBody(Class<T> bodyType) {
165-
return this.clientResponse.bodyTo(bodyType);
166-
}
167-
168-
@Nullable
169-
public <T> T getBody(ParameterizedTypeReference<T> bodyType) {
170-
return this.clientResponse.bodyTo(bodyType);
162+
/**
163+
* Provide access to the response. For internal use to decode the body.
164+
*/
165+
ConvertibleClientHttpResponse getClientResponse() {
166+
return this.clientResponse;
171167
}
172168

173169
/**

spring-test/src/test/java/org/springframework/test/web/servlet/client/samples/bind/RouterFunctionTests.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,17 @@
1616

1717
package org.springframework.test.web.servlet.client.samples.bind;
1818

19+
import java.nio.charset.StandardCharsets;
20+
1921
import org.junit.jupiter.api.BeforeEach;
2022
import org.junit.jupiter.api.Test;
2123

24+
import org.springframework.test.web.servlet.client.EntityExchangeResult;
2225
import org.springframework.test.web.servlet.client.RestTestClient;
2326
import org.springframework.web.servlet.function.RouterFunction;
2427
import org.springframework.web.servlet.function.ServerResponse;
2528

29+
import static org.assertj.core.api.Assertions.assertThat;
2630
import static org.springframework.web.servlet.function.RequestPredicates.GET;
2731
import static org.springframework.web.servlet.function.RouterFunctions.route;
2832

@@ -50,4 +54,15 @@ void test() {
5054
.expectBody(String.class).isEqualTo("It works!");
5155
}
5256

57+
@Test
58+
void testEntityExchangeResult() {
59+
EntityExchangeResult<byte[]> result = this.testClient.get().uri("/test")
60+
.exchange()
61+
.expectStatus().isOk()
62+
.expectBody()
63+
.returnResult();
64+
65+
assertThat(result.getResponseBody()).isEqualTo("It works!".getBytes(StandardCharsets.UTF_8));
66+
}
67+
5368
}

0 commit comments

Comments
 (0)