Testing JSON Requests with Rest Assured and Jackson API

In the world of API testing, handling JSON is a vital aspect. Rest Assured, a popular Java library for API testing, offers seamless integration with the Jackson API to work with JSON objects efficiently. Jackson, a high-performance JSON library, allows easy conversion between Java objects and JSON. In this blog post, we'll explore how to test JSON requests using Rest Assured and Jackson API.


Why Use Jackson API with Rest Assured?

Jackson simplifies working with JSON by:

  • Converting Java objects into JSON (Serialization).
  • Reading JSON into Java objects (Deserialization).
  • Providing excellent integration with frameworks like Rest Assured.

Combining Rest Assured and Jackson ensures efficient and readable API tests.


Setting Up the Environment

1. Maven Dependencies

Add the following dependencies to your pom.xml file:

<dependencies>
    <!-- Rest Assured -->
    <dependency>
        <groupId>io.rest-assured</groupId>
        <artifactId>rest-assured</artifactId>
        <version>5.3.0</version>
    </dependency>
    <!-- Jackson Databind -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.15.2</version>
    </dependency>
</dependencies>

2. Sample API Endpoint
 We’ll use a dummy endpoint https://jsonplaceholder.typicode.com/posts for demonstration.    You can replace it with your actual API.

Testing JSON Request with Jackson

Step 1: Create a POJO Class

The first step is to create a Plain Old Java Object (POJO) class that represents your JSON structure.

public class Post {

    private int userId;

    private int id;

    private String title;

    private String body;


    // Getters and Setters

    public int getUserId() {

        return userId;

    }


    public void setUserId(int userId) {

        this.userId = userId;

    }


    public int getId() {

        return id;

    }


    public void setId(int id) {

        this.id = id;

    }


    public String getTitle() {

        return title;

    }


    public void setTitle(String title) {

        this.title = title;

    }


    public String getBody() {

        return body;

    }


    public void setBody(String body) {

        this.body = body;

    }

}

Step 2: Serialize Java Object to JSON

Before sending the request, you need to serialize the Java object into a JSON string.

import com.fasterxml.jackson.databind.ObjectMapper;


public class SerializeExample {

    public static void main(String[] args) throws Exception {

        // Create a Post object

        Post post = new Post();

        post.setUserId(1);

        post.setId(101);

        post.setTitle("Jackson with Rest Assured");

        post.setBody("This is a test post.");


        // Convert Post object to JSON string

        ObjectMapper objectMapper = new ObjectMapper();

        String json = objectMapper.writeValueAsString(post);


        System.out.println("Serialized JSON: " + json);

    }

}

Step 3: Send JSON Request with Rest Assured

Use the serialized JSON in your API test.

import io.restassured.RestAssured;

import io.restassured.response.Response;

import static io.restassured.RestAssured.given;


public class RestAssuredExample {

    public static void main(String[] args) throws Exception {

        // Base URI

        RestAssured.baseURI = "https://jsonplaceholder.typicode.com";


        // Create a Post object

        Post post = new Post();

        post.setUserId(1);

        post.setId(101);

        post.setTitle("Jackson with Rest Assured");

        post.setBody("This is a test post.");


        // Serialize Post object to JSON

        ObjectMapper objectMapper = new ObjectMapper();

        String json = objectMapper.writeValueAsString(post);


        // Send POST request

        Response response = given()

                .header("Content-Type", "application/json")

                .body(json)

                .when()

                .post("/posts")

                .then()

                .statusCode(201) // Validate response status

                .extract().response();


        // Print response

        System.out.println("Response: " + response.asString());

    }

}

Step 4: Deserialize JSON Response

After receiving the response, you can deserialize the JSON into a Java object.

import io.restassured.response.Response;


public class DeserializeExample {

    public static void main(String[] args) throws Exception {

        // Make GET request to fetch a post

        Response response = RestAssured.get("https://jsonplaceholder.typicode.com/posts/1");


        // Deserialize JSON response to Post object

        ObjectMapper objectMapper = new ObjectMapper();

        Post post = objectMapper.readValue(response.asString(), Post.class);


        // Print details

        System.out.println("Title: " + post.getTitle());

        System.out.println("Body: " + post.getBody());

    }

}

Key Benefits

  1. Readability: POJOs make tests more structured and readable.
  2. Reusability: Use the same POJO class for multiple endpoints.
  3. Scalability: Jackson handles complex nested JSON structures with ease.

Followers