From 7e79d0e20620046f0c93247a27545a96a5cd445b Mon Sep 17 00:00:00 2001 From: rstoyanchev Date: Wed, 25 Feb 2026 10:58:14 +0000 Subject: [PATCH] Polishing in DefaultRestClientTests --- .../web/client/DefaultRestClientTests.java | 48 ++++++++----------- 1 file changed, 20 insertions(+), 28 deletions(-) diff --git a/spring-web/src/test/java/org/springframework/web/client/DefaultRestClientTests.java b/spring-web/src/test/java/org/springframework/web/client/DefaultRestClientTests.java index 44cfc95742d..814f3c3a2eb 100644 --- a/spring-web/src/test/java/org/springframework/web/client/DefaultRestClientTests.java +++ b/spring-web/src/test/java/org/springframework/web/client/DefaultRestClientTests.java @@ -47,6 +47,11 @@ import static org.mockito.Mockito.verify; */ class DefaultRestClientTests { + private static final String URL = "https://example.com"; + + public static final String BODY = "Hello World"; + + private final ClientHttpRequestFactory requestFactory = mock(); private final ClientHttpRequest request = mock(); @@ -66,70 +71,57 @@ class DefaultRestClientTests { @Test void requiredBodyWithClass() throws IOException { - mockSentRequest(HttpMethod.GET, "https://example.org"); + mockSentRequest(HttpMethod.GET, URL); mockResponseStatus(HttpStatus.OK); - mockResponseBody("Hello World", MediaType.TEXT_PLAIN); + mockResponseBody(BODY, MediaType.TEXT_PLAIN); - String result = this.client.get() - .uri("https://example.org") - .retrieve() - .requiredBody(String.class); + String result = this.client.get().uri(URL).retrieve().requiredBody(String.class); - assertThat(result).isEqualTo("Hello World"); + assertThat(result).isEqualTo(BODY); } @Test void requiredBodyWithClassAndNullBody() throws IOException { - mockSentRequest(HttpMethod.GET, "https://example.org"); + mockSentRequest(HttpMethod.GET, URL); mockResponseStatus(HttpStatus.OK); mockEmptyResponseBody(); assertThatIllegalStateException().isThrownBy(() -> - this.client.get() - .uri("https://example.org") - .retrieve() - .requiredBody(String.class) + this.client.get().uri(URL).retrieve().requiredBody(String.class) ); } @Test void requiredBodyWithParameterizedTypeReference() throws IOException { - mockSentRequest(HttpMethod.GET, "https://example.org"); + mockSentRequest(HttpMethod.GET, URL); mockResponseStatus(HttpStatus.OK); - mockResponseBody("Hello World", MediaType.TEXT_PLAIN); + mockResponseBody(BODY, MediaType.TEXT_PLAIN); - String result = this.client.get() - .uri("https://example.org") - .retrieve() + String result = this.client.get().uri(URL).retrieve() .requiredBody(new ParameterizedTypeReference<>() {}); - assertThat(result).isEqualTo("Hello World"); + assertThat(result).isEqualTo(BODY); } @Test void requiredBodyWithParameterizedTypeReferenceAndNullBody() throws IOException { - mockSentRequest(HttpMethod.GET, "https://example.org"); + mockSentRequest(HttpMethod.GET, URL); mockResponseStatus(HttpStatus.OK); mockEmptyResponseBody(); assertThatIllegalStateException().isThrownBy(() -> - this.client.get() - .uri("https://example.org") - .retrieve() + this.client.get().uri(URL).retrieve() .requiredBody(new ParameterizedTypeReference() {}) ); } @Test void inputStreamBody() throws IOException { - mockSentRequest(HttpMethod.GET, "https://example.org"); + mockSentRequest(HttpMethod.GET, URL); mockResponseStatus(HttpStatus.OK); - mockResponseBody("Hello World", MediaType.TEXT_PLAIN); + mockResponseBody(BODY, MediaType.TEXT_PLAIN); - InputStream result = this.client.get() - .uri("https://example.org") - .retrieve() - .requiredBody(InputStream.class); + InputStream result = this.client.get().uri(URL).retrieve().requiredBody(InputStream.class); assertThat(result).isInstanceOf(InputStream.class); verify(this.response, times(0)).close();