Play with RestAssured

How REST API works?

Here are the key components of a REST API:

  1. Resources: A resource is a piece of data or functionality that is exposed by the API. Resources are typically identified by a unique URL, or endpoint, which is used to access and manipulate the resource.

  2. HTTP Verbs: HTTP verbs, also known as methods, are used to perform operations on the resources. The most commonly used HTTP verbs are GET (to retrieve a resource), POST (to create a new resource), PUT (to update an existing resource), and DELETE (to delete a resource).

  3. Headers: HTTP headers are used to provide additional information about the request or response. Headers can be used to specify the content type, authentication information, and other metadata about the request or response.

  4. Request and Response Bodies: Request and response bodies are used to send and receive data between the client and the server. Request bodies are used to send data to the server, while response bodies are used to return data to the client.

  5. Status Codes: HTTP status codes are used to indicate the status of the request or response. Common status codes include 200 (OK), 201 (Created), 400 (Bad Request), 401 (Unauthorized), 404 (Not Found), and 500 (Internal Server Error).

Why RestAsssured ?

RestAssured is a Java-based library used for API testing, simplifies API testing by providing a DSL (Domain-Specific Language) that allows you to write readable and concise tests. Some of the essential features of RestAssured include:

  • Support for different HTTP methods (GET, POST, PUT, DELETE, etc.)

  • Ability to specify request headers, query parameters, and request bodies.

  • Assertions for response codes, response headers, response body, and response time.

  • Support for authentication mechanisms such as basic, digest, and OAuth.

  • Support for testing RESTful web services that use JSON or XML data formats.

Before running with RestAssured, let's know some basic API testing and its type.

  1. Types of APIs: There are different types of APIs, including REST, SOAP, and GraphQL. REST APIs are the most commonly used, and they use HTTP methods to interact with web resources.

  2. HTTP methods: APIs use HTTP methods such as GET, POST, PUT, and DELETE to perform operations on web resources. Each HTTP method has a specific purpose and can be used to retrieve, create, update, or delete data.

  3. Response codes: APIs return response codes that indicate the success or failure of a request. Some common response codes include 200 (OK), 201 (Created), 400 (Bad Request), 401 (Unauthorized), 403 (Forbidden), 404 (Not Found), and 500 (Internal Server Error).

  4. Authentication mechanisms: APIs may require authentication to access certain resources. Some common authentication mechanisms include basic authentication, digest authentication, OAuth, and JWT.

  5. Request and response headers: HTTP requests and responses contain headers that provide additional information about the request or response. Some common request headers include Content-Type, Accept, and Authorization, while response headers may include Content-Type, Content-Length, and Cache-Control.

  6. Request and response body: The request body contains data sent to the API in a POST or PUT request, while the response body contains the data returned by the API.

  7. Testing techniques: API testing techniques include functional testing, load testing, security testing, and boundary testing.

How I can install RestAssured in the Maven project?

Open the pom.XML file and add the below dependency

<dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>rest-assured</artifactId>
    <version>5.3.0</version>
    <scope>test</scope>
</dependency>

In the test class, we need the below static import

import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;

public class DerivApiTest {

    @Test
    public void testGetStatusCode() {
        given().baseUri("https://reqres.in").queryParam("page", "2").when().get("/api/users/2").then().assertThat()
                .statusCode(200);

    }

    @Test
    public void testGetResponseContentType() {
        given().baseUri("https://reqres.in").queryParam("page", "2").when().get("/api/users/2").then()
                .contentType("application/json");

    }

    @Test
    public void testGetResponseBodyContains() {
        given().baseUri("https://reqres.in").queryParam("page", "2").when().get("/api/users/2").then().assertThat()
                .body("data.first_name", is("Janet"));

    }

}

Few open source API providers for the testing.

  • OpenWeatherMap

  • PokéAPI

  • JSONPlaceholder

  • The Movie Database (TMDb)

  • Random User Generator

  • NASA API