Skip to content

Commit 0b6239c

Browse files
committed
Improve diagnostics for failures in RestTemplateIntegrationTests
1 parent 7d6be2e commit 0b6239c

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

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

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,16 @@
5656

5757
import static org.assertj.core.api.Assertions.assertThat;
5858
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
59+
import static org.assertj.core.api.Assertions.fail;
5960
import static org.junit.Assume.assumeFalse;
6061
import static org.springframework.http.HttpMethod.POST;
6162

6263
/**
64+
* Integration tests for {@link RestTemplate}.
65+
*
6366
* @author Arjen Poutsma
6467
* @author Brian Clozel
68+
* @author Sam Brannen
6569
*/
6670
@RunWith(Parameterized.class)
6771
public class RestTemplateIntegrationTests extends AbstractMockWebServerTestCase {
@@ -72,7 +76,7 @@ public class RestTemplateIntegrationTests extends AbstractMockWebServerTestCase
7276
public ClientHttpRequestFactory clientHttpRequestFactory;
7377

7478
@SuppressWarnings("deprecation")
75-
@Parameters
79+
@Parameters(name = "{0}")
7680
public static Iterable<? extends ClientHttpRequestFactory> data() {
7781
return Arrays.asList(
7882
new SimpleClientHttpRequestFactory(),
@@ -225,7 +229,7 @@ public void multipart() throws UnsupportedEncodingException {
225229
Resource logo = new ClassPathResource("/org/springframework/http/converter/logo.jpg");
226230
parts.add("logo", logo);
227231

228-
template.postForLocation(baseUrl + "/multipart", parts);
232+
convertHttpServerErrorToAssertionError(() -> template.postForLocation(baseUrl + "/multipart", parts));
229233
}
230234

231235
@Test
@@ -235,7 +239,7 @@ public void form() throws UnsupportedEncodingException {
235239
form.add("name 2", "value 2+1");
236240
form.add("name 2", "value 2+2");
237241

238-
template.postForLocation(baseUrl + "/form", form);
242+
convertHttpServerErrorToAssertionError(() -> template.postForLocation(baseUrl + "/form", form));
239243
}
240244

241245
@Test
@@ -315,6 +319,30 @@ public void postWithoutBody() throws Exception {
315319
}
316320

317321

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+
318346
public interface MyJacksonView1 {}
319347

320348
public interface MyJacksonView2 {}

0 commit comments

Comments
 (0)