30
30
import com .fasterxml .jackson .annotation .JsonTypeName ;
31
31
import com .fasterxml .jackson .annotation .JsonView ;
32
32
import org .junit .Before ;
33
+ import org .junit .Rule ;
33
34
import org .junit .Test ;
35
+ import org .junit .rules .TestRule ;
36
+ import org .junit .runner .Description ;
34
37
import org .junit .runner .RunWith ;
35
38
import org .junit .runners .Parameterized ;
36
39
import org .junit .runners .Parameterized .Parameter ;
37
40
import org .junit .runners .Parameterized .Parameters ;
41
+ import org .junit .runners .model .Statement ;
38
42
39
43
import org .springframework .core .ParameterizedTypeReference ;
40
44
import org .springframework .core .io .ClassPathResource ;
56
60
57
61
import static org .assertj .core .api .Assertions .assertThat ;
58
62
import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
59
- import static org .assertj .core .api .Assertions .fail ;
60
63
import static org .junit .Assume .assumeFalse ;
61
64
import static org .springframework .http .HttpMethod .POST ;
62
65
@@ -72,6 +75,40 @@ public class RestTemplateIntegrationTests extends AbstractMockWebServerTestCase
72
75
73
76
private RestTemplate template ;
74
77
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
+
75
112
@ Parameter
76
113
public ClientHttpRequestFactory clientHttpRequestFactory ;
77
114
@@ -229,7 +266,7 @@ public void multipart() throws UnsupportedEncodingException {
229
266
Resource logo = new ClassPathResource ("/org/springframework/http/converter/logo.jpg" );
230
267
parts .add ("logo" , logo );
231
268
232
- convertHttpServerErrorToAssertionError (() -> template .postForLocation (baseUrl + "/multipart" , parts ) );
269
+ template .postForLocation (baseUrl + "/multipart" , parts );
233
270
}
234
271
235
272
@ Test
@@ -239,7 +276,7 @@ public void form() throws UnsupportedEncodingException {
239
276
form .add ("name 2" , "value 2+1" );
240
277
form .add ("name 2" , "value 2+2" );
241
278
242
- convertHttpServerErrorToAssertionError (() -> template .postForLocation (baseUrl + "/form" , form ) );
279
+ template .postForLocation (baseUrl + "/form" , form );
243
280
}
244
281
245
282
@ Test
@@ -319,30 +356,6 @@ public void postWithoutBody() throws Exception {
319
356
}
320
357
321
358
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
-
346
359
public interface MyJacksonView1 {}
347
360
348
361
public interface MyJacksonView2 {}
0 commit comments