RestAssured #19 - Creating POJO With Lombok (Serialization & De-Serialization)
What is Lombok?
Lombok is a Java library that aims to reduce boilerplate code in Java classes by providing annotations to automatically generate common methods such as getters, setters, and constructors.
Using Lombok annotations in Java classes can significantly reduce the amount of repetitive code that developers need to write, making the codebase cleaner and more maintainable. It simplifies the process of creating Plain Old Java Objects (POJOs) by generating the required methods during compilation.
Some of the common Lombok annotations and their purposes are:
@Data: Generates getter, setter, equals, hashCode, and toString methods for all fields in the class.
@Getter and @Setter: Generates getter and setter methods for the fields.
@AllArgsConstructor: Generates a constructor with arguments for all fields.
@NoArgsConstructor: Generates a default constructor with no arguments.
@ToString: Generates a toString method for the class.
@EqualsAndHashCode: Generates equals and hashCode methods based on the fields.
@Builder: Implements the builder pattern to create instances of the class with a fluent API.
CreateCrocodilesTest
Endpoint - https://test-api.k6.io/my/crocodiles/
POJO With Lombok
Add below dependency for Lombok
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.28</version>
<scope>provided</scope>
</dependency>
Create POJO for crocodiles
Here I have created POJO both for creating crocodiles and validating that why there are 2 extra fields age and id which are part of the response body.
@Data
@NoArgsConstructor
@AllArgsConstructor
public class CrocodilesPOJOUsingLombok {
@JsonProperty("name")
private String crocodileName;
@JsonProperty("sex")
private String gender;
@JsonProperty("date_of_birth")
private String crocodileDOB;
@JsonProperty("age")
private int age;
@JsonProperty("id")
private int id;
public CrocodilesPOJOUsingLombok(String crocodileName,String gender,String crocodileDOB){
this.crocodileName = crocodileName;
this.gender = gender;
this.crocodileDOB = crocodileDOB;
}
}
In the above code, I have created a constructor for fields needed for POST requests.
Creating Crocodile using POJO and validating response from POJO
@Test
public void createCrocodilesUsingPOJOTest(){
String crocodileName = "Test Crocodile";
String gender = "M";
String dob = "2010-06-27";
int age = 13;
CrocodilesPOJOUsingLombok crocodilesPOJOUsingLombok = new CrocodilesPOJOUsingLombok(crocodileName,gender,dob);
RestAssured.baseURI = "https://test-api.k6.io/";
Response response = RestAssured.given()
.contentType(ContentType.JSON)
.header("Authorization","Bearer "+authToken)
.body(crocodilesPOJOUsingLombok)
.when()
.post("/my/crocodiles/");
response.prettyPrint();
Assert.assertEquals(response.statusCode(),201);
CrocodilesPOJOUsingLombok crocodilesResponse = response.as(CrocodilesPOJOUsingLombok.class);
Assert.assertEquals(crocodilesResponse.getCrocodileName(),crocodileName);
Assert.assertEquals(crocodilesResponse.getGender(),gender);
Assert.assertEquals(crocodilesResponse.getCrocodileDOB(),dob);
Assert.assertEquals(crocodilesResponse.getAge(),age);
}
Subscribe to my newsletter
Read articles from SUBODH SINGH directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
SUBODH SINGH
SUBODH SINGH
An Automation Engineer, experienced in the automation of front-end and back-end applications with 11+ years of experience. Experience in designing, developing, and implementing automation frameworks. I like to learn and share knowledge on test automation.