TestRestTemplate

open class 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

Since

1.4.0

Constructors

Link copied to clipboard
constructor(restTemplateBuilder: RestTemplateBuilder)
Create a new TestRestTemplate instance.
constructor(httpClientOptions: Array<TestRestTemplate.HttpClientOption>)
Create a new TestRestTemplate instance.
constructor(username: String, password: String, httpClientOptions: Array<TestRestTemplate.HttpClientOption>)
Create a new TestRestTemplate instance with the specified credentials.
constructor(builder: RestTemplateBuilder, username: String, password: String, httpClientOptions: Array<TestRestTemplate.HttpClientOption>)
Create a new TestRestTemplate instance with the specified credentials.

Types

Link copied to clipboard
Options used to customize the Apache HTTP Client.

Properties

Link copied to clipboard

Functions

Link copied to clipboard
open fun delete(url: URI)
Delete the resources at the specified URL.
open fun delete(url: String, urlVariables: Array<Any>)
open fun delete(url: String, urlVariables: Map<String, out Any>)
Delete the resources at the specified URI.
Link copied to clipboard
open fun <T> exchange(requestEntity: RequestEntity<out Any>, responseType: Class<T>): ResponseEntity<T>
open fun <T> exchange(requestEntity: RequestEntity<out Any>, responseType: ParameterizedTypeReference<T>): ResponseEntity<T>
Execute the request specified in the given RequestEntity and return the response as ResponseEntity.
open fun <T> exchange(url: URI, method: HttpMethod, requestEntity: HttpEntity<out Any>, responseType: Class<T>): ResponseEntity<T>
open fun <T> exchange(url: URI, method: HttpMethod, requestEntity: HttpEntity<out Any>, responseType: ParameterizedTypeReference<T>): ResponseEntity<T>
open fun <T> exchange(url: String, method: HttpMethod, requestEntity: HttpEntity<out Any>, responseType: Class<T>, urlVariables: Array<Any>): ResponseEntity<T>
open fun <T> exchange(url: String, method: HttpMethod, requestEntity: HttpEntity<out Any>, responseType: Class<T>, urlVariables: Map<String, out Any>): ResponseEntity<T>
open fun <T> exchange(url: String, method: HttpMethod, requestEntity: HttpEntity<out Any>, responseType: ParameterizedTypeReference<T>, urlVariables: Array<Any>): ResponseEntity<T>
open fun <T> exchange(url: String, method: HttpMethod, requestEntity: HttpEntity<out Any>, responseType: ParameterizedTypeReference<T>, urlVariables: Map<String, out Any>): ResponseEntity<T>
Execute the HTTP method to the given URI template, writing the given request entity to the request, and returns the response as ResponseEntity.
Link copied to clipboard
inline fun <T : Any> TestRestTemplate.exchange(requestEntity: RequestEntity<*>): ResponseEntity<T>
inline fun <T : Any> TestRestTemplate.exchange(url: URI, method: HttpMethod, requestEntity: HttpEntity<*>? = null): ResponseEntity<T>
inline fun <T : Any> TestRestTemplate.exchange(url: String, method: HttpMethod, requestEntity: HttpEntity<*>? = null, vararg uriVariables: Any): ResponseEntity<T>
inline fun <T : Any> TestRestTemplate.exchange(url: String, method: HttpMethod, requestEntity: HttpEntity<*>? = null, uriVariables: Map<String, *>): ResponseEntity<T>

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.

Link copied to clipboard
open fun <T> execute(url: URI, method: HttpMethod, requestCallback: RequestCallback, responseExtractor: ResponseExtractor<T>): T
Execute the HTTP method to the given URL, preparing the request with the RequestCallback, and reading the response with a ResponseExtractor.
open fun <T> execute(url: String, method: HttpMethod, requestCallback: RequestCallback, responseExtractor: ResponseExtractor<T>, urlVariables: Array<Any>): T
open fun <T> execute(url: String, method: HttpMethod, requestCallback: RequestCallback, responseExtractor: ResponseExtractor<T>, urlVariables: Map<String, out Any>): T
Execute the HTTP method to the given URI template, preparing the request with the RequestCallback, and reading the response with a ResponseExtractor.
Link copied to clipboard
open fun <T> getForEntity(url: URI, responseType: Class<T>): ResponseEntity<T>
Retrieve a representation by doing a GET on the URL .
open fun <T> getForEntity(url: String, responseType: Class<T>, urlVariables: Array<Any>): ResponseEntity<T>
Retrieve an entity by doing a GET on the specified URL.
open fun <T> getForEntity(url: String, responseType: Class<T>, urlVariables: Map<String, out Any>): ResponseEntity<T>
Retrieve a representation by doing a GET on the URI template.
Link copied to clipboard
inline fun <T : Any> TestRestTemplate.getForEntity(url: String, vararg uriVariables: Any): ResponseEntity<T>
inline fun <T : Any> TestRestTemplate.getForEntity(url: String, uriVariables: Map<String, *>): ResponseEntity<T>

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.

Link copied to clipboard
open fun <T> getForObject(url: URI, responseType: Class<T>): T
Retrieve a representation by doing a GET on the URL .
open fun <T> getForObject(url: String, responseType: Class<T>, urlVariables: Array<Any>): T
Retrieve a representation by doing a GET on the specified URL.
open fun <T> getForObject(url: String, responseType: Class<T>, urlVariables: Map<String, out Any>): T
Retrieve a representation by doing a GET on the URI template.
Link copied to clipboard
inline fun <T : Any> TestRestTemplate.getForObject(url: URI): T?
inline fun <T : Any> TestRestTemplate.getForObject(url: String, vararg uriVariables: Any): T?
inline fun <T : Any> TestRestTemplate.getForObject(url: String, uriVariables: Map<String, Any?>): T?

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.

Link copied to clipboard
open fun getRootUri(): String
Returns the root URI applied by rootUri or "" if the root URI has not been applied.
Link copied to clipboard
Retrieve all headers of the resource specified by the URL.
open fun headForHeaders(url: String, urlVariables: Array<Any>): HttpHeaders
open fun headForHeaders(url: String, urlVariables: Map<String, out Any>): HttpHeaders
Retrieve all headers of the resource specified by the URI template.
Link copied to clipboard
Return the value of the Allow header for the given URL.
open fun optionsForAllow(url: String, urlVariables: Array<Any>): Set<HttpMethod>
open fun optionsForAllow(url: String, urlVariables: Map<String, out Any>): Set<HttpMethod>
Return the value of the Allow header for the given URI.
Link copied to clipboard
open fun <T> patchForObject(url: URI, request: Any, responseType: Class<T>): T
Update a resource by PATCHing the given object to the URL, and returns the representation found in the response.
open fun <T> patchForObject(url: String, request: Any, responseType: Class<T>, uriVariables: Array<Any>): T
open fun <T> patchForObject(url: String, request: Any, responseType: Class<T>, uriVariables: Map<String, out Any>): T
Update a resource by PATCHing the given object to the URI template, and returns the representation found in the response.
Link copied to clipboard
inline fun <T : Any> TestRestTemplate.patchForObject(url: URI, request: Any? = null): T?
inline fun <T : Any> TestRestTemplate.patchForObject(url: String, request: Any? = null, vararg uriVariables: Any): T?
inline fun <T : Any> TestRestTemplate.patchForObject(url: String, request: Any? = null, uriVariables: Map<String, *>): T?

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.

Link copied to clipboard
open fun <T> postForEntity(url: URI, request: Any, responseType: Class<T>): ResponseEntity<T>
Create a new resource by POSTing the given object to the URL, and returns the response as ResponseEntity.
open fun <T> postForEntity(url: String, request: Any, responseType: Class<T>, urlVariables: Array<Any>): ResponseEntity<T>
Create a new resource by POSTing the given object to the URI template, and returns the response as ResponseEntity.
open fun <T> postForEntity(url: String, request: Any, responseType: Class<T>, urlVariables: Map<String, out Any>): ResponseEntity<T>
Create a new resource by POSTing the given object to the URI template, and returns the response as HttpEntity.
Link copied to clipboard
inline fun <T : Any> TestRestTemplate.postForEntity(url: URI, request: Any? = null): ResponseEntity<T>
inline fun <T : Any> TestRestTemplate.postForEntity(url: String, request: Any? = null, vararg uriVariables: Any): ResponseEntity<T>
inline fun <T : Any> TestRestTemplate.postForEntity(url: String, request: Any? = null, uriVariables: Map<String, *>): ResponseEntity<T>

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.

Link copied to clipboard
open fun postForLocation(url: URI, request: Any): URI
Create a new resource by POSTing the given object to the URL, and returns the value of the Location header.
open fun postForLocation(url: String, request: Any, urlVariables: Array<Any>): URI
open fun postForLocation(url: String, request: Any, urlVariables: Map<String, out Any>): URI
Create a new resource by POSTing the given object to the URI template, and returns the value of the Location header.
Link copied to clipboard
open fun <T> postForObject(url: URI, request: Any, responseType: Class<T>): T
Create a new resource by POSTing the given object to the URL, and returns the representation found in the response.
open fun <T> postForObject(url: String, request: Any, responseType: Class<T>, urlVariables: Array<Any>): T
open fun <T> postForObject(url: String, request: Any, responseType: Class<T>, urlVariables: Map<String, out Any>): T
Create a new resource by POSTing the given object to the URI template, and returns the representation found in the response.
Link copied to clipboard
inline fun <T : Any> TestRestTemplate.postForObject(url: URI, request: Any? = null): T?
inline fun <T : Any> TestRestTemplate.postForObject(url: String, request: Any? = null, vararg uriVariables: Any): T?
inline fun <T : Any> TestRestTemplate.postForObject(url: String, request: Any? = null, uriVariables: Map<String, *>): T?

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.

Link copied to clipboard
open fun put(url: URI, request: Any)
Creates a new resource by PUTting the given object to URL.
open fun put(url: String, request: Any, urlVariables: Array<Any>)
Create or update a resource by PUTting the given object to the URI.
open fun put(url: String, request: Any, urlVariables: Map<String, out Any>)
Creates a new resource by PUTting the given object to URI template.
Link copied to clipboard
Configure the UriTemplateHandler to use to expand URI templates.
Link copied to clipboard
open fun withBasicAuth(username: String, password: String): TestRestTemplate
Creates a new TestRestTemplate with the same configuration as this one, except that it will send basic authorization headers using the given username and password.