REST Assured vs Apache HttpClient and RestTemplate
Upasana | September 12, 2020 | 2 min read | 8,083 views
REST Assured is a high level Java DSL for simplified testing of REST based services built over HTTP. On the other hand, HttpClient is a low level client for simplifying Http Communication, In-fact HttpClient is used by REST Assured under the hood for Http communication.
REST Assured and HttpClient are designed to solve different set of problems.
What does HttpClient do?
HttpClient sends request to and get response from server over HTTP protocol, and also it takes care of the following stuff:
HTTP protocol interception
Secure HTTP connections - SSL/TLS
HTTP proxy server handling
Handles HTTP cookies
Connection pooling for different hosts, keep alive strategy,
multi-threaded request execution
REST Assured is a Java DSL for simplifying testing of REST based services and is built on top of HttpClient. It offers the following additional capabilities:
Validating REST API response using inbuilt Hemcrest Matchers
JSON & XML serialization and deserialization
Extracting JSON data using JsonPath and XML data using XmlPath
Verifying response body, cookies, headers, content-type and http status
Authentication using Basic Auth, Digest Auth, Form Authentication (CSRF support), OAuth (OAuth1 and OAuth2)
verifying multi-part form data
Request and response logging for easy troubleshooting
Spring Mock Mvc Module
Spring Web Test Client Module
RestTemplate is also a high level REST client which uses HttpClient under the hood, but it is mostly used in development rather than testing. It lacks most of the testing related features readily available in REST Assured like - Assertion capabilities - inbuilt Hemcrest matchers support, ease of use from testing perspective, out of the box support for various authentication protocols, ease of logging requests response, measuring request time, etc.
Spring WebClient was introduced in Spring Boot 2 for reactive programming. It has following advantages over conventional RestTemplate:
It is non-blocking & reactive in nature, so you can achieve more throughput with limited threads. This should definitely reduce the hardware requirements.
It has more functional feel. WebClient has a functional, fluent API with reactive types for declarative composition.
It has all the features that RestTemplate has to offer in cloud native environment (Load balancing, token relay, oauth2 handling, etc.)
In all future developments based on Spring Boot 2+, WebClient should be preferred for calling remote REST services over RestTemplate.
If you are planning to do functional testing for your REST Endpoints, REST Assured might be the better choice than using HttpClient or RestTemplate.
For microservices development with Spring Boot 2, where you want to call remote REST service endpoints from one service to another, WebClient might be the best choice.
Top articles in this category:
- Rest Assured API Testing Interview Questions
- REST Assured Basic Authentication
- REST Assured with plain/text response body
- RestAssured multipart file upload
- SDET: JUnit interview questions for automation engineer
- 50 SDET Java Interview Questions & Answers
- OAuth2 protected resources in RestAssured Testcases
Recommended books for interview preparation:
Find more on this topic:
SDET Java Interview pattern and collection of questions covering SDET coding challenges, automation testing concepts, functional, api, integration, performance and security testing, junit5, testng, jmeter, selenium and rest assured
Last updated 1 week ago