3

I am writing tests for my spring boot application. When writing tests for error responses I noticed that TestRestTemplate client throws parse exception (it's error response, so format is different that standard response) for the following test:

@Test
fun `post should return 404 if object does not exists`() {
    // expect
    restTemplate.exchange(
        localUrl("/api/v1/data/not_exists/"),
        HttpMethod.POST,
        RequestClass(data = randomString()),
        ResponseClass::class.java
    )
}
org.springframework.web.client.RestClientException: Error while extracting response for type

According to spring documentation HttpClientErrorException error should be thrown. https://docs.spring.io/spring/docs/3.0.6.RELEASE_to_3.1.0.BUILD-SNAPSHOT/3.0.6.RELEASE/org/springframework/web/client/HttpClientErrorException.html

There is an ugly solution where I cant set response type to Nothing, but error response content will be lost.

restTemplate.exchange(
    localUrl("/api/v1/data/not_exists/"),
    HttpMethod.POST,
    RequestClass(data = randomString()),
    Nothing::class.java
)

Is this possible to make TestRestTemplate/RestTemplate to throw actual HttpClientErrorException instead of failing on parsing response?

Blejwi
  • 1,005
  • 2
  • 10
  • 25
  • https://stackoverflow.com/questions/68134196/parsing-an-exception-instead-of-list-of-entities-is-not-working-when-using-test similar question – Joergi Jun 28 '21 at 07:34

0 Answers0