16

What is the difference between RestTemplate and its test version? When we do exception handling through @ControllerAdvice, RestTemplate is throwing the exception, but for same flow test version is returning json containing exception details.

So, I wanted to see the summary of differences between them.

Saikat
  • 14,222
  • 20
  • 104
  • 125
krmanish007
  • 6,749
  • 16
  • 58
  • 100

1 Answers1

17

The restTemplate give you more possibility, testRestTemplate is only a wrapper of restTemplate which offers you convenient approach, like you said, it doesn't throw exception, but wrap it with json response, such behavior should be implemented by yourself in the real application, but you may not care in the test.

here is the javadoc from testRestTemplate

/**
 * Convenient subclass of {@link RestTemplate} that is suitable for integration tests.
 * They are fault tolerant, and optionally can carry Basic authentication headers. If
 * Apache Http Client 4.3.2 or better is available (recommended) it will be used as the
 * client, and by default configured to ignore cookies and redirects.
 *
 * @author Dave Syer
 * @author Phillip Webb
 */

The similar pattern can be found by ReflectionTestUtils and ReflectionUtils

Saikat
  • 14,222
  • 20
  • 104
  • 125
Jaiwo99
  • 9,687
  • 3
  • 36
  • 53
  • 1
    do we have exact list of functionality difference between them somewhere, other than what you already mentioned? – krmanish007 May 17 '16 at 09:18
  • I found this @krmanish007 https://www.baeldung.com/spring-boot-testresttemplate Look at the section: 4. What's New in TestRestTemplate? – Ismail Yavuz Jan 06 '23 at 12:28