Exception when Using TestRestTemplate Exception when Using TestRestTemplate spring spring

Exception when Using TestRestTemplate


You need the Apache HTTP client to be able to handle errors yourself with TestRestTemplate. This worked for me with your project

    <dependency>        <groupId>org.apache.httpcomponents</groupId>        <artifactId>httpclient</artifactId>        <scope>test</scope>    </dependency>

(I also removed the duplicated junit, mockito and spring-test dependencies.)


I don't think Jackson will know how to convert JSON to Object. Try Map or something?


This solved my issue.

Reference : https://github.com/spring-projects/spring-framework/issues/21321

restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());         restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {       public boolean hasError(ClientHttpResponse response) throws IOException{           HttpStatus statusCode = response.getStatusCode();            return statusCode.series() == HttpStatus.Series.SERVER_ERROR;        }     });