Improve diagnostics for failures in RestTemplateIntegrationTests

This commit is contained in:
Sam Brannen
2019-06-26 15:16:54 +03:00
parent 7d6be2e26e
commit 0b6239cecc

View File

@@ -56,12 +56,16 @@ import org.springframework.util.MultiValueMap;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.fail;
import static org.junit.Assume.assumeFalse;
import static org.springframework.http.HttpMethod.POST;
/**
* Integration tests for {@link RestTemplate}.
*
* @author Arjen Poutsma
* @author Brian Clozel
* @author Sam Brannen
*/
@RunWith(Parameterized.class)
public class RestTemplateIntegrationTests extends AbstractMockWebServerTestCase {
@@ -72,7 +76,7 @@ public class RestTemplateIntegrationTests extends AbstractMockWebServerTestCase
public ClientHttpRequestFactory clientHttpRequestFactory;
@SuppressWarnings("deprecation")
@Parameters
@Parameters(name = "{0}")
public static Iterable<? extends ClientHttpRequestFactory> data() {
return Arrays.asList(
new SimpleClientHttpRequestFactory(),
@@ -225,7 +229,7 @@ public class RestTemplateIntegrationTests extends AbstractMockWebServerTestCase
Resource logo = new ClassPathResource("/org/springframework/http/converter/logo.jpg");
parts.add("logo", logo);
template.postForLocation(baseUrl + "/multipart", parts);
convertHttpServerErrorToAssertionError(() -> template.postForLocation(baseUrl + "/multipart", parts));
}
@Test
@@ -235,7 +239,7 @@ public class RestTemplateIntegrationTests extends AbstractMockWebServerTestCase
form.add("name 2", "value 2+1");
form.add("name 2", "value 2+2");
template.postForLocation(baseUrl + "/form", form);
convertHttpServerErrorToAssertionError(() -> template.postForLocation(baseUrl + "/form", form));
}
@Test
@@ -315,6 +319,30 @@ public class RestTemplateIntegrationTests extends AbstractMockWebServerTestCase
}
/**
* Execute the supplied {@code Runnable}, and if it throws an
* {@link HttpServerErrorException}, rethrow it wrapped in an {@link AssertionError}
* with the {@link HttpServerErrorException#getResponseBodyAsString() response body}
* as the error message.
*
* <p>This mechanism provides an actually meaningful failure message if the
* test fails.
*/
private static void convertHttpServerErrorToAssertionError(Runnable runnable) {
try {
runnable.run();
}
catch (HttpServerErrorException ex) {
String responseBody = ex.getResponseBodyAsString();
String prefix = "java.lang.AssertionError: ";
if (responseBody.startsWith(prefix)) {
responseBody = responseBody.substring(prefix.length());
}
fail(responseBody, ex);
}
}
public interface MyJacksonView1 {}
public interface MyJacksonView2 {}