TestRestTemplate
Convenient alternative of RestTemplate that is suitable for integration tests. TestRestTemplate
is fault-tolerant. This means that 4xx and 5xx do not result in an exception being thrown and can instead be detected through the response entity and its status code.
A TestRestTemplate
can optionally 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.
Note: To prevent injection problems this class intentionally does not extend RestTemplate. If you need access to the underlying RestTemplate use getRestTemplate.
If you are using the @SpringBootTest annotation with an embedded server, a TestRestTemplate is automatically available and can be @Autowired
into your test. If you need customizations (for example to adding additional message converters) use a RestTemplateBuilder@Bean
.
Author
Dave Syer
Phillip Webb
Andy Wilkinson
Kristine Jetzke
Dmytro Nosan
Yanming Zhou
Since
1.4.0
Constructors
Types
Functions
Extension for TestRestTemplate.exchange providing an exchange<Foo>(...)
variant leveraging Kotlin reified type parameters. This extension is not subject to type erasure and retains actual generic type arguments.
Extension for TestRestTemplate.getForEntity providing a getForEntity<Foo>(...)
variant leveraging Kotlin reified type parameters. Like the original Java method, this extension is subject to type erasure. Use exchange if you need to retain actual generic type arguments.
Extension for TestRestTemplate.getForObject providing a getForObject<Foo>(...)
variant leveraging Kotlin reified type parameters. Like the original Java method, this extension is subject to type erasure. Use exchange if you need to retain actual generic type arguments.
""
if the root URI has not been applied.Allow
header for the given URL.Allow
header for the given URI.Extension for TestRestTemplate.patchForObject providing a patchForObject<Foo>(...)
variant leveraging Kotlin reified type parameters. Like the original Java method, this extension is subject to type erasure. Use exchange if you need to retain actual generic type arguments.
Extension for TestRestTemplate.postForEntity providing a postForEntity<Foo>(...)
variant leveraging Kotlin reified type parameters. Like the original Java method, this extension is subject to type erasure. Use exchange if you need to retain actual generic type arguments.
Location
header.Location
header.Extension for TestRestTemplate.postForObject providing a postForObject<Foo>(...)
variant leveraging Kotlin reified type parameters. Like the original Java method, this extension is subject to type erasure. Use exchange if you need to retain actual generic type arguments.
TestRestTemplate
with the same configuration as this one, except that it will send basic authorization headers using the given username
and password
.TestRestTemplate
with the same configuration as this one, except that it will customize the ClientHttpRequestFactorySettings.TestRestTemplate
with the same configuration as this one, except that it will apply the given ClientHttpRequestFactorySettings.