title: TestRestTemplate throws exception for 4xx status codes
body: <p>I am writing component tests for a Spring-Boot application, to test my security configuration. I am therefore running tests that should test for both successful responses as well as "forbidden" status.
  The problem I'm encountering is that because my REST call expects a complex JSON, for blocked calls the tests fail because TestRestTemplate is trying to deserialize a response body that is not there.</p>
  
  <p>I am running a Spring-Boot application, and the tests class is annotated with:</p>
  
  <pre><code>@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
  </code></pre>
  
  <p>I am trying to test a REST API that should return a list of users.
  A simplified version of the call would be:</p>
  
  <pre><code>ResponseEntity&lt;List&lt;User&gt;&gt; responseEntity  = testRestTemplate.exchange(URL, HttpMethod.GET, entity, new ParameterizedTypeReference&lt;List&lt;User&gt;&gt;() {});
  </code></pre>
  
  <p>where the TestRestTemplate is autowired by Spring, and the entity contains the authorization information.</p>
  
  <p>For unauthorized request, I am getting an error like:</p>
  
  <pre><code>org.springframework.web.client.RestClientException: Error while extracting response for type [java.util.List&lt;my.package.User&gt;] and content type [application/json;charset=UTF-8]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `java.util.ArrayList` out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.ArrayList` out of START_OBJECT token
   at [Source: (PushbackInputStream); line: 1, column: 1]
  </code></pre>
  
  <p>If I change the response entity to receive String instead of List, I receive the response and can check the status and see that it is "forbidden"</p>
  
  <pre><code>ResponseEntity&lt;String&gt; responseEntity  = testRestTemplate.exchange(URL, HttpMethod.GET, null, String.class);
  </code></pre>
  
  <p>I know I can work around this by: </p>
  
  <ul>
  <li>Using String and deserializing with Gson, or </li>
  <li>Using RestTemplate instead of TestRestTemplate and handling the HttpStatusCodeException, or </li>
  <li>Overriding methods to not try to deserialize when status code is not 2xx</li>
  </ul>
  
  <p>but since TestRestTemplate is supposed to be a fault-tolerant convenience subclass, I would have expected it to out-of-the-box not try to deserialize on error response.</p>
  
  <p>Am I missing something here? Am I using it wrong?</p>