Skip to content

Commit 8e1cb9a

Browse files
committed
Improve diagnostics for all server failures in RestTemplateIntegrationTests
1 parent d522e98 commit 8e1cb9a

File tree

1 file changed

+40
-27
lines changed

1 file changed

+40
-27
lines changed

spring-web/src/test/java/org/springframework/web/client/RestTemplateIntegrationTests.java

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,15 @@
3030
import com.fasterxml.jackson.annotation.JsonTypeName;
3131
import com.fasterxml.jackson.annotation.JsonView;
3232
import org.junit.Before;
33+
import org.junit.Rule;
3334
import org.junit.Test;
35+
import org.junit.rules.TestRule;
36+
import org.junit.runner.Description;
3437
import org.junit.runner.RunWith;
3538
import org.junit.runners.Parameterized;
3639
import org.junit.runners.Parameterized.Parameter;
3740
import org.junit.runners.Parameterized.Parameters;
41+
import org.junit.runners.model.Statement;
3842

3943
import org.springframework.core.ParameterizedTypeReference;
4044
import org.springframework.core.io.ClassPathResource;
@@ -56,7 +60,6 @@
5660

5761
import static org.assertj.core.api.Assertions.assertThat;
5862
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
59-
import static org.assertj.core.api.Assertions.fail;
6063
import static org.junit.Assume.assumeFalse;
6164
import static org.springframework.http.HttpMethod.POST;
6265

@@ -72,6 +75,40 @@ public class RestTemplateIntegrationTests extends AbstractMockWebServerTestCase
7275

7376
private RestTemplate template;
7477

78+
/**
79+
* Custom JUnit 4 rule that executes the supplied {@code Statement} in a
80+
* try-catch block.
81+
*
82+
* <p>If the statement throws an {@link HttpServerErrorException}, this rule will
83+
* throw an {@link AssertionError} that wraps the {@code HttpServerErrorException}
84+
* using the {@link HttpServerErrorException#getResponseBodyAsString() response body}
85+
* as the failure message.
86+
*
87+
* <p>This mechanism provides an actually meaningful failure message if the
88+
* test fails due to an {@code AssertionError} on the server.
89+
*/
90+
@Rule
91+
public TestRule serverErrorToAssertionErrorConverter = (Statement next, Description description) -> {
92+
return new Statement() {
93+
94+
@Override
95+
public void evaluate() throws Throwable {
96+
try {
97+
next.evaluate();
98+
}
99+
catch (HttpServerErrorException ex) {
100+
String responseBody = ex.getResponseBodyAsString();
101+
String prefix = AssertionError.class.getName() + ": ";
102+
if (responseBody.startsWith(prefix)) {
103+
responseBody = responseBody.substring(prefix.length());
104+
}
105+
throw new AssertionError(responseBody, ex);
106+
}
107+
}
108+
};
109+
};
110+
111+
75112
@Parameter
76113
public ClientHttpRequestFactory clientHttpRequestFactory;
77114

@@ -229,7 +266,7 @@ public void multipart() throws UnsupportedEncodingException {
229266
Resource logo = new ClassPathResource("/org/springframework/http/converter/logo.jpg");
230267
parts.add("logo", logo);
231268

232-
convertHttpServerErrorToAssertionError(() -> template.postForLocation(baseUrl + "/multipart", parts));
269+
template.postForLocation(baseUrl + "/multipart", parts);
233270
}
234271

235272
@Test
@@ -239,7 +276,7 @@ public void form() throws UnsupportedEncodingException {
239276
form.add("name 2", "value 2+1");
240277
form.add("name 2", "value 2+2");
241278

242-
convertHttpServerErrorToAssertionError(() -> template.postForLocation(baseUrl + "/form", form));
279+
template.postForLocation(baseUrl + "/form", form);
243280
}
244281

245282
@Test
@@ -319,30 +356,6 @@ public void postWithoutBody() throws Exception {
319356
}
320357

321358

322-
/**
323-
* Execute the supplied {@code Runnable}, and if it throws an
324-
* {@link HttpServerErrorException}, rethrow it wrapped in an {@link AssertionError}
325-
* with the {@link HttpServerErrorException#getResponseBodyAsString() response body}
326-
* as the error message.
327-
*
328-
* <p>This mechanism provides an actually meaningful failure message if the
329-
* test fails.
330-
*/
331-
private static void convertHttpServerErrorToAssertionError(Runnable runnable) {
332-
try {
333-
runnable.run();
334-
}
335-
catch (HttpServerErrorException ex) {
336-
String responseBody = ex.getResponseBodyAsString();
337-
String prefix = "java.lang.AssertionError: ";
338-
if (responseBody.startsWith(prefix)) {
339-
responseBody = responseBody.substring(prefix.length());
340-
}
341-
fail(responseBody, ex);
342-
}
343-
}
344-
345-
346359
public interface MyJacksonView1 {}
347360

348361
public interface MyJacksonView2 {}

0 commit comments

Comments
 (0)