简体   繁体   中英

Integration-testing Spring's RestTemplate with MockRestServiceServer - using returned body despite 500 error

I'm trying to write an integration test of a RESTful service that, in certain circumstances, responds with a 500 server error but still returns a useful response body with a message detailing the problem. In order to get this response body, my code uses a custom error handler, something like:

public class Allow500ResponseErrorHandler implements ResponseErrorHandler {
  @Override
  public boolean hasError(ClientHttpResponse response) throws IOException {
    HttpStatus statusCode = response.getStatusCode();
    return !HttpStatus.OK.equals(statusCode)
      && !HttpStatus.INTERNAL_SERVER_ERROR.equals(statusCode);
  }

  @Override
  public void handleError(ClientHttpResponse response) throws IOException {
    log.error("Unexpected response code: {} {}", response.getStatusCode(),
        response.getStatusText());
  }
}

This works fine, as does the integration test for a "healthy" response:

@Test
public void testHealthyResponse() {
  MockRestServiceServer mockServer = MockRestServiceServer.createServer(restTemplate);
  mockServer.expect(requestTo("/someUri")).andExpect(method(HttpMethod.GET))
      .andRespond(withSuccess(HEALTHY_RESPONSE, MediaType.APPLICATION_JSON));
  restTemplate.setErrorHandler(new Allow500ResponseErrorHandler());

  MyCustomResult result = restTemplate
      .exchange("/someUri", HttpMethod.GET, null,
          new ParameterizedTypeReference<MyCustomResult>() {
          }).getBody();
  // assertThat(... tests go here...);

  mockServer.verify();
}

(Where HEALTHY_RESPONSE of course is a static expected healthy response in JSON.)

But even if I set the same error handler in my integration test for the "unhealthy" response, I get

org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class my.package.MyCustomResult] and content type [application/octet-stream]
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:110)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:809)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:793)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:572)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:530)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:476)
at [my test code]

Setup is like the above, except the expectation is:

mockServer.expect(requestTo("/someUri")).andExpect(method(HttpMethod.GET))
    .andRespond(withServerError().body(UNHEALTHY_RESPONSE));

(This is using spring-web-4.1.7's RestTemplate and spring-test-4.1.7's MockRestServiceServer .)

How do I simulate a server error response with a useful body ? What am I missing?

Got it working - the error message was a little misleading, but the and content type [application/octet-stream] should've given me a hint.

All I needed to do (doh) was set the media type on the expectation:

mockServer.expect(requestTo("/someUri")).andExpect(method(HttpMethod.GET))
  .andRespond(withServerError().body(UNHEALTHY_RESPONSE)
  .contentType(MediaType.APPLICATION_JSON));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM