REST Assured vs Apache HttpClient and RestTemplate

Carvia Tech | September 12, 2020 | 2 min read | 6,573 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

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

  • Session Filters

  • Spring Mock Mvc Module

  • Spring Web Test Client Module

  • Kotlin support

RestTemplate

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

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.)

  • You can refer to official documentation for more details.

In all future developments based on Spring Boot 2+, WebClient should be preferred for calling remote REST services over RestTemplate.

Verdict

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:
  1. Rest Assured API Testing Interview Questions
  2. SDET: JUnit interview questions for automation engineer
  3. 50 SDET Java Interview Questions & Answers
  4. SDET Java Coding Challenges
  5. REST Assured Basic Authentication
  6. RestAssured multipart file upload
  7. OAuth2 protected resources using RestTemplate


Find more on this topic:
SDET Interviews image
SDET Interviews

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


Recommended books for interview preparation:

This website uses cookies to ensure you get the best experience on our website. more info