Testing APIs using advanced REST client — Part I

Anton Smirnov
ITNEXT
Published in
4 min readJun 8, 2019

--

In this article, I would like to review the variety of REST clients.

In my examples I will use the resource https://jsonplaceholder.typicode.com/

JSONPlaceholder is a free online REST API that you can use whenever you need some fake data.
It’s great for tutorials, testing new libraries, sharing code examples.

REST client is an application/library that allows using REST API to communicate with a remote server.

REST(RESTful) — is the General principle of application/site organization and interaction with server through the HTTP Protocol. The peculiarity of REST is that server does not remember state of the user between requests — in each request, information is transmitted that identifies the user (for example, token obtained through Oauth-authorization) and all parameters necessary to perform operation.

REST API is a set of remote calls to standard methods that return data in a specific format.

1. REST Assured is most common Java library for REST api test automation. This library makes REST testing with Java similar to the Groovy approach. It is a powerful library, which allows you to create tests, that will be more readable and easy to maintain.

@Test
public void getCorrectData() {
given.get("https://jsonplaceholder.typicode.com/users/1")
.then()
.statusCode(200)
.and()
.assertThat()
.body("name", equalTo("Leanne Graham"))
.body("username", equalTo("Bret"))
.body("email", equalTo("Sincere@april.biz"));
}

2. Spring REST Template. If you are constantly working with Java on your project and you know what is Spring framework , you have probably heard about existence of this library.

Spring is a powerful framework. REST Template is a great library with which you can create a REST client.

@Test
public void getCorrectData() {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
httpHeaders.add("User-Agent", "chrome");
HttpEntity httpEntity = new HttpEntity(httpHeaders);
ResponseEntity<String> responseEntity = restTemplate.exchange("https://jsonplaceholder.typicode.com/users/1", GET, httpEntity, String.class);
assertThat(responseEntity.getStatusCode(), equalTo(HttpStatus.OK));
}

3. Retrofit is a well-known library for networking among Android developers. Some developers even consider it is a kind of standard in web development/test automation. There are a lot of reasons for such popularity:

3.1 This library perfectly supports REST api.

3.2 It is easy to test and configure.

3.3 Requests over network with its help are carried out of quite simply.

One of the key library features — the ability to perform both synchronous and asynchronous calls.

@Test
public interface getCorrectData {
@GET("https://jsonplaceholder.typicode.com/users")
public Call<Post> getPostWithID(@Path("id") int id);
}

4. Postman — is not a framework and is not a library, it is a simple plugin for Chrome browser. It is easy and convenient client which can make requests directly from the browser.

The main purpose of Postman is to create collections of API requests. Any tester can easily understand the work of the service by opening the collection.

The main concept which operates Postman’s Collection on the upper level and Request on the bottom. All request begin with a collection and comes down to describing your API using queries.

Collection is the starting point for new API. You can consider a collection as a part of project file.

Folder is used to combine queries into one group within a collection.

Request is the main component of a collection. Request is created in the Constructor. Query designer is the main space you should work with.

Postman is able to execute requests using all standard HTTP methods. The tester can easily change or add necessary headers, cookies, and request body.

This figure shows the sequence of query execution in Postman:

“Pre-request Script” is used to perform the necessary operations before the request. For example, you can make a query to another system and use the result of its execution in the main query. “Tests script” is used to write:

  • Tests.
  • Check the results.
  • Saving test result to variables.

Here is the query sequence with script folders and collections:

Postman allows you to create scripts at the folder and collection levels. They are also called — Pre-request Script” and “Test Script”. They will be executed before and after each request in the folder or in the entire collection.

I will explain in details Rest Assured In the next part.

--

--

I’m a software engineer who specializes in testing and automation. My top languages are Java and Swift. My blog is https://test-engineer.tech/