question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make is easier to use TestRestTemplate with a custom ResponseErrorHandler

See original GitHub issue

Custom error handler set using RestTemplateBuilder bean or using the configuration TestRestTemplateConfiguration from Spring Boot Reference Guide is overriden by TestRestTemplate.NoOpResponseErrorHandler.

Specifically, when using the following class:

class CustomResponseErrorHandler extends DefaultResponseErrorHandler {
    // implementation
}

in the configuration of the builder:

ResponseErrorHandler customErrorHandler = new CustomResponseErrorHandler();
RestTemplateBuilder builder = builderProvider.getIfAvailable()
                                             .errorHandler(customErrorHandler);

The following line is false:

new TestRestTemplate(builder.build()).getRestTemplate().getErrorHandler().equals(customErrorHandler)

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
cs94njwcommented, Feb 17, 2022

TestRestTemplate doesn’t actually offer what @bclozel describes. I am writing my unit tests, using TestRestTemplate with a ResponseType of “UUID”. When I test BAD_REQUEST errors from my service, instead of my being able to inspect the ResponseEntity and check for status “401”, TestRestTemplate throws an exception saying it can’t decode the error message as a UUID. I don’t understand why TestRestTemplate is even attempting to decode the ResponseType when a 4xx error is returned.

0reactions
bclozelcommented, Feb 17, 2022

@cs94njw I don’t understand how this is related. This looks like a different problem? Could you report that separately with a sample project (something we can git clone or download) we can take a look at?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Spring Resttemplate exception handling - Stack Overflow
You want to create a class that implements ResponseErrorHandler and then use an instance of it to set the error handling of your...
Read more >
RestTemplate Error Handling Example - Apps Developer Blog
Create a custom exception that we want to throw,; To enable RestTemplate to handle errors, we will implement ResponseErrorHandler interface, ...
Read more >
Spring RestTemplate Error Handling - Baeldung
In this short tutorial, we'll discuss how to implement and inject the ResponseErrorHandler interface in a RestTemplate instance to ...
Read more >
Spring Boot RestTemplate Error Handling
The simplest way to add a custom error handler is to use a try-catch block to catch the HttpStatusCodeException exception.
Read more >
Complete Guide to Spring RestTemplate - Reflectoring
In this tutorial, we will understand how to use RestTemplate for ... each of the HTTP methods which make it easy to invoke...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found