56
56
57
57
import static org .assertj .core .api .Assertions .assertThat ;
58
58
import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
59
+ import static org .assertj .core .api .Assertions .fail ;
59
60
import static org .junit .Assume .assumeFalse ;
60
61
import static org .springframework .http .HttpMethod .POST ;
61
62
62
63
/**
64
+ * Integration tests for {@link RestTemplate}.
65
+ *
63
66
* @author Arjen Poutsma
64
67
* @author Brian Clozel
68
+ * @author Sam Brannen
65
69
*/
66
70
@ RunWith (Parameterized .class )
67
71
public class RestTemplateIntegrationTests extends AbstractMockWebServerTestCase {
@@ -72,7 +76,7 @@ public class RestTemplateIntegrationTests extends AbstractMockWebServerTestCase
72
76
public ClientHttpRequestFactory clientHttpRequestFactory ;
73
77
74
78
@ SuppressWarnings ("deprecation" )
75
- @ Parameters
79
+ @ Parameters ( name = "{0}" )
76
80
public static Iterable <? extends ClientHttpRequestFactory > data () {
77
81
return Arrays .asList (
78
82
new SimpleClientHttpRequestFactory (),
@@ -225,7 +229,7 @@ public void multipart() throws UnsupportedEncodingException {
225
229
Resource logo = new ClassPathResource ("/org/springframework/http/converter/logo.jpg" );
226
230
parts .add ("logo" , logo );
227
231
228
- template .postForLocation (baseUrl + "/multipart" , parts );
232
+ convertHttpServerErrorToAssertionError (() -> template .postForLocation (baseUrl + "/multipart" , parts ) );
229
233
}
230
234
231
235
@ Test
@@ -235,7 +239,7 @@ public void form() throws UnsupportedEncodingException {
235
239
form .add ("name 2" , "value 2+1" );
236
240
form .add ("name 2" , "value 2+2" );
237
241
238
- template .postForLocation (baseUrl + "/form" , form );
242
+ convertHttpServerErrorToAssertionError (() -> template .postForLocation (baseUrl + "/form" , form ) );
239
243
}
240
244
241
245
@ Test
@@ -315,6 +319,30 @@ public void postWithoutBody() throws Exception {
315
319
}
316
320
317
321
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
+
318
346
public interface MyJacksonView1 {}
319
347
320
348
public interface MyJacksonView2 {}
0 commit comments