Serialization and Deserialization in Java for Test Automation

Dinesh Y SDinesh Y S
3 min read

🚀 Introduction

As a test automation engineer working with APIs, you often send data to the server and receive data in response. That data usually comes in JSON or XML formats. But how do we convert between Java objects and these formats?

This is where serialization and deserialization come in.

🔄 What is Serialization?

Serialization is the process of converting a Java object into a format (like JSON) that can be stored or transmitted.

👉 Imagine converting a Java User object into a JSON string to send in an API request.

✏️ Example:

import com.fasterxml.jackson.databind.ObjectMapper;

User user = new User("Dinesh", 25);
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(user);

System.out.println(json); // {"name":"Dinesh","age":25}

🔃 What is Deserialization?

Deserialization is the opposite — converting JSON (or other data formats) into Java objects.

👉 Think of this as reading a JSON response from an API and turning it into a usable Java object.

✏️ Example:

String responseJson = "{\"name\":\"Dinesh\",\"age\":25}";
User userObj = mapper.readValue(responseJson, User.class);

System.out.println(userObj.getName()); // Dinesh

📦 Maven Dependency

To use Jackson for converting between Java objects and JSON, add the following dependency in your pom.xml:

<!-- Jackson for JSON serialization/deserialization -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.16.1</version>
</dependency>

Once added, you can use ObjectMapper to serialize and deserialize your Java objects:

ObjectMapper mapper = new ObjectMapper();

✅ Note: jackson-databind internally pulls in jackson-core and jackson-annotations, so you usually don’t need to include them separately.

👨‍💻 Real Use Case in API Automation (Rest Assured)

When writing test cases with Rest Assured:

  • Send Java object → Serialize it to JSON in request body.

  • Read response → Deserialize JSON into Java class for assertions.

📌 Sample DTO:

public class User {
    private String name;
    private int age;

    //  Getters, Setters
    public String getName(){
    return name;
    }
    public void setName(String name){
    this.name = name;
    }
    public int getAge(){
    return age;
    }
    public void setAge(int age){
    this.age = age;
    }
}

📨 Sending Serialized Data:

User user = new User();
user.setName("john");
user.setAge("25");

given()
    .contentType("application/json")
    .body(user)
.when()
    .post("/api/users")
.then()
    .statusCode(201);

📥 Deserializing Response:

User responseUser =
    given()
        .get("/api/users/1")
    .then()
        .extract()
        .as(User.class);

System.out.println(responseUser.getName());

🧰 Libraries Commonly Used

  • Jackson – Default with Rest Assured

  • Gson – Google’s JSON library

  • org.json – Lightweight but limited

⚠️ Gotchas to Watch For

  • Always include getters and setters in your POJO.

  • Use annotations like @JsonProperty if JSON keys don’t match field names.

  • Handle nulls and default values gracefully.


✅ When Should You Use It?

  • Automating POST/PUT API requests

  • Verifying API response bodies

  • Building data-driven tests with Java objects

  • Ensuring compatibility with backend contracts


🧠 Final Thoughts

Serialization and deserialization aren't just technical necessities—they’re the backbone of clean, maintainable API tests. Mastering this allows you to:

  • Simplify test data creation

  • Improve reusability

  • Handle complex JSON structures with ease

0
Subscribe to my newsletter

Read articles from Dinesh Y S directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Dinesh Y S
Dinesh Y S

Automation Engineer | Java + Selenium | Appium + Java | RestAssured | Sharing real-world automation tips