TA #1 — Java API clients for automation testing

Nazar Khimin
2 min readDec 14, 2019

--

[TA] — Test Automation article series

Based on existing open-source clients, the most popular are RestAssured, Spring RestTemplate, Apache HttpComponents. And I had a question which one to use, what pros and cons.

Spring RestTemplate

Intro: Synchronous client to perform HTTP requests, exposing a simple, template method API over underlying HTTP client libraries such as the JDK HttpURLConnection, Apache HttpComponents, and others.

RestTemplate id build over clear API clients likes Apache HttpComponents, OkHttp and takes care of the transformation from JSON or XML to Java objects. These HTTP clients take care of all low-level details of communication via HTTP.

NOTE: As of 5.0, The RestTemplate will be deprecated in a future version and will not have major new features added going forward. But since this version Spring offers org.springframework.web.reactive.client.WebClient that efficient support for both sync and async calls ( i.e when you do a rest call you need to wait till the response comes back to proceed further), as well as streaming scenarios. It comes from Spring Webflux - reactive programming which based on event-driven design.

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.

Inference

it is mostly used in development rather than testing.

Apache HttpComponents project

One of the major submodule HttpCore that is a set of low-level HTTP transport components that can be used to build custom client and server-side HTTP services with a minimal footprint.

HttpCore sends the request to and gets a response from the 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

Apache HttpClient has been replaced by the Apache HttpComponents project

Inference

it is mostly used in development rather than testing.

Rest-Assured

REST-assured is build over the Apache HTTP Client for Http communication. It was designed to simplify the testing and validation of REST APIs.

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

Inference

Perfect suitable for automation functional testing.

Conclusion

If you are planning to do functional testing for your REST Endpoints, REST Assured might be the better choice than using HttpClient or RestTemplate.

Sources:

https://springframework.guru/using-resttemplate-with-apaches-httpclient/

https://stackoverflow.com/questions/47974757/webclient-vs-resttemplate?rq=1

--

--

No responses yet