Java API Testing with RestAssured

Java API Testing with RestAssured
Photo by Thomas Ciszewski / Unsplash

RestAssured is a popular Java-based testing framework that is widely used for testing RESTful APIs. It provides several features that make it easy to write and execute API tests. Here are some of the key features of RestAssured:

  1. Simple and Intuitive: RestAssured provides a simple and intuitive syntax that makes it easy to write API tests in Java. Its DSL (Domain-Specific Language) makes it easy to read and write test cases, even for those who are new to API testing.
  2. Request and Response Specification: RestAssured provides a mechanism to define request and response specifications which can be used across multiple tests. This helps in reducing the code redundancy and makes the test code more modular and maintainable.
  3. Flexible Verification: RestAssured provides a flexible and extensible way of verifying the responses returned from an API. It allows developers to use a wide range of assertions for validating different aspects of the response such as status codes, response headers, response body, response time etc.
  4. Support for Authentication: RestAssured provides built-in support for handling authentication mechanisms such as basic, digest, and OAuth. This makes it easy to test APIs that require authentication.
  5. Support for Serialization and Deserialization: RestAssured provides support for serialization and deserialization of request and response payloads. It supports multiple formats such as JSON, XML, and YAML.
  6. Integration with popular Testing Frameworks: RestAssured integrates well with popular testing frameworks like JUnit and TestNG. This enables developers to seamlessly incorporate API tests into their existing test suites.

Overall, RestAssured is a powerful testing framework that makes it easy to write and execute API tests. Its intuitive syntax, flexible verification, and built-in support for authentication and serialization make it a popular choice among Java developers for API testing.

Here's an example of a simple test code written using RestAssured that tests a GET API endpoint:

import org.junit.Test;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import static org.junit.Assert.assertEquals;

public class APITest {

    @Test
    public void testGetRequest() {
        // Set the base URL for the API endpoint
        RestAssured.baseURI = "https://jsonplaceholder.typicode.com";

        // Send a GET request to the /posts endpoint
        Response response = RestAssured.get("/posts");

        // Check that the response status code is 200 (OK)
        assertEquals(response.getStatusCode(), 200);

        // Check that the response contains the expected number of posts
        assertEquals(response.jsonPath().getList("$").size(), 100);
    }
}

In this example, we are testing a simple GET request to the "https://jsonplaceholder.typicode.com/posts" endpoint. We set the base URL using the RestAssured.baseURI method, and then send a GET request to the "/posts" endpoint using the RestAssured.get method.

We then use the assertEquals method to check that the response status code is 200 (OK) and that the response contains 100 posts (which is the expected number of posts for this endpoint). We use the jsonPath method to extract the response body as a JSON object, and then use the getList method to retrieve a list of all the posts returned in the response.

RestAssured provides a wide range of assertions that can be used to validate different aspects of an API response. Here are some examples of commonly used assertions:

  1. Checking Response Status: We can use the statusCode method to check the status code of the API response. For example, assertThat().statusCode(200) will check if the status code of the response is 200 (OK).
  2. Checking Response Body: We can use the body method to check the contents of the response body. For example, assertThat().body("name", equalTo("John")) will check if the "name" field in the response body is equal to "John".
  3. Checking Response Headers: We can use the header method to check the headers of the response. For example, assertThat().header("Content-Type", "application/json") will check if the "Content-Type" header in the response is equal to "application/json".
  4. Checking Response Time: We can use the time method to check the response time of the API. For example, assertThat().time(lessThan(5000L)) will check if the response time is less than 5000 milliseconds.
  5. Checking Response Cookies: We can use the cookie method to check the cookies of the response. For example, assertThat().cookie("sessionId", "12345") will check if the "sessionId" cookie in the response is equal to "12345".