{"id":75281,"date":"2025-09-09T16:02:05","date_gmt":"2025-09-09T10:32:05","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=75281"},"modified":"2025-09-23T10:50:15","modified_gmt":"2025-09-23T05:20:15","slug":"pojo-implementation-in-api-testing-automation-using-rest-assured","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/pojo-implementation-in-api-testing-automation-using-rest-assured\/","title":{"rendered":"POJO implementation in API testing (Automation) using Rest Assured"},"content":{"rendered":"<p><strong>Introduction<\/strong>:-<\/p>\n<p>A straightforward Java class called POJO (Plain Old Java Object) stores data object-orientedly and is independent of frameworks. POJOs simplify, tidy up, and reuse payload handling. We can improve the readability, resilience, and maintainability of our script by implementing the POJO in API automation. Java objects are automatically converted to JSON\/XML by serialization, and API responses are automatically converted into Java objects by deserialization, eliminating the need for manual parsing.<\/p>\n<p><strong>What you will learn in this blog:<\/strong><\/p>\n<ol>\n<li>POJO Implementation in API Automation.<\/li>\n<li>Serialization\/De-Serialization.<\/li>\n<li>Writing the test script to automate the POST API call with POJO.<\/li>\n<li>Writing the assertion in the deserialized API response.<\/li>\n<\/ol>\n<p><strong>Tech Stack used in this blog:<\/strong><\/p>\n<p>1. For this demo, we will use an open API endpoint &#8220;http:\/\/httpbin.org\/post.&#8221;<br \/>\n2. JAVA 21.0.4.<br \/>\n3. Rest Assured library, version 4.5.1<br \/>\n4. Jackson-databind library, version 2.15.0<br \/>\n5. TestNG, version 7.7.1<\/p>\n<p><strong>Let\u2019s get started with the actual implementation in the code:<\/strong><\/p>\n<p>For this demo, we will use an open API endpoint (http:\/\/httpbin.org\/post), which uses the following data as a payload.<br \/>\n<strong>{<\/strong><\/p>\n<p>&#8220;firstname&#8221;: &#8220;Mark&#8221;,<br \/>\n&#8220;lastname&#8221;: &#8220;Flower&#8221;,<br \/>\n&#8220;gender&#8221;: &#8220;Male&#8221;,<br \/>\n&#8220;age&#8221;: 40,<br \/>\n&#8220;salary&#8221;: 500000<br \/>\n<strong>}<\/strong><br \/>\n<strong>Move 1:-<\/strong><\/p>\n<p><strong>First, we will create a class with the name \u201cEmployee\u201d and declare all the payload\u2019s fields as private variables, and following that, we will create the getter and setter for all keys<br \/>\n<\/strong>public class Employee<br \/>\n<strong>{<\/strong><\/p>\n<p>private String firstname;<br \/>\nprivate String lastname;<br \/>\nprivate String gender;<br \/>\nprivate int age;<br \/>\nprivate double salary;<\/p>\n<p>public String getFirstname() {<br \/>\nreturn firstname;<br \/>\n}<br \/>\npublic void setFirstname(String firstname) {<br \/>\nthis.firstname = firstname;<br \/>\n}<br \/>\npublic String getLastname() {<br \/>\nreturn lastname;<br \/>\n}<br \/>\npublic void setLastname(String lastname) {<br \/>\nthis.lastname = lastname;<br \/>\n}<br \/>\npublic String getGender() {<br \/>\nreturn gender;<br \/>\n}<br \/>\npublic void setGender(String gender) {<br \/>\nthis.gender = gender;<br \/>\n}<br \/>\npublic int getAge() {<br \/>\nreturn age;<br \/>\n}<br \/>\npublic void setAge(int age) {<br \/>\nthis.age = age;<br \/>\n}<br \/>\npublic double getSalary() {<br \/>\nreturn salary;<br \/>\n}<br \/>\npublic void setSalary(double salary) {<br \/>\nthis.salary = salary;<br \/>\n}<br \/>\n<strong>}<\/strong><\/p>\n<p><strong>Move 2:- Creating the test class in which the actual test script will be written.<\/strong><\/p>\n<p>public class <strong>Test<\/strong><\/p>\n<p><strong>{<br \/>\n\/\/ Import Statements for this Test class<br \/>\n<\/strong>import org.testng.Assert;<br \/>\nimport org.testng.annotations.Test;<br \/>\nimport com.fasterxml.jackson.core.JsonProcessingException;<br \/>\nimport com.fasterxml.jackson.databind.ObjectMapper;<br \/>\nimport io.restassured.RestAssured;<br \/>\nimport io.restassured.http.ContentType;<br \/>\nimport io.restassured.response.Response;<br \/>\nimport io.restassured.specification.RequestSpecification;<\/p>\n<p><strong><br \/>\n\/\/ Step 1:\u00a0 Creating the object of the Employee class and setting the values in the API\u2019s payload<\/strong><\/p>\n<p>Employee\u00a0 <strong>ObjEmployee1<\/strong> = new Employee () ;<\/p>\n<p><strong>ObjEmployee1<\/strong>.setFirstname(&#8220;Mark&#8221;);<br \/>\n<strong>ObjEmployee1<\/strong>.setLastname(&#8220;Flower&#8221;);<br \/>\n<strong>ObjEmployee1<\/strong>.setGender(&#8220;Male&#8221;);<br \/>\n<strong>ObjEmployee1<\/strong>.setAge(40);<br \/>\n<strong>ObjEmployee1<\/strong>.setSalary(50000);<\/p>\n<p><strong>\/\/ Step 2: Converting Employee Class Objects to JSON as a String with object mapper<\/strong><\/p>\n<p>ObjectMapper\u00a0 <strong>Obj_ObjectMapper<\/strong> = new ObjectMapper ();<br \/>\nString <strong>EmployeeData<\/strong>\u00a0 =\u00a0 <strong>Obj_ObjectMapper<\/strong>.writerWithDefaultPrettyPrinter().writeValueAsString(<strong>ObjEmployee1<\/strong>);<br \/>\nSystem.out.println(<strong>EmployeeData)<\/strong>;<\/p>\n<p><strong>\u00a0\/\/ Step 3: Writing the script for the post call and using the converted\u00a0 JSON from Step 2 as an API payload.<\/strong><\/p>\n<p>RequestSpecification <strong>requestSpec\u00a0<\/strong> = RestAssured.given();<br \/>\n<strong>requestSpec<\/strong>.baseUri(&#8220;http:\/\/httpbin.org\/post&#8221;);<br \/>\n<strong>requestSpec<\/strong>.contentType(ContentType.JSON);<br \/>\n<strong>requestSpec<\/strong>.body(<strong>EmployeeData<\/strong>);<br \/>\nResponse <strong>resp\u00a0<\/strong> = requestSpec.post();<br \/>\n<strong>resp<\/strong>.prettyPrint();<br \/>\nAssert.assertEquals(<strong>resp<\/strong>.statusCode(), 200,&#8221;Check for status code&#8221;);<\/p>\n<p>\/\/ <strong>Step 4 : Convert JSON String (employeeJSON) to Class object (Employee)\u00a0<\/strong><\/p>\n<p>Employee <strong>ObjEmployee2<\/strong> =\u00a0 Obj_ObjectMapper.readValue(EmployeeData, Employee.class);<\/p>\n<p>String <strong>firstName\u00a0<\/strong> = <strong>ObjEmployee2<\/strong>.getFirstname();<br \/>\nString <strong>lastName\u00a0<\/strong> = <strong>ObjEmployee2<\/strong>.getLastname();<br \/>\nint\u00a0 \u00a0 \u00a0<strong>empAge\u00a0<\/strong>\u00a0 \u00a0 \u00a0= <strong>ObjEmployee2<\/strong>.getAge() ;<\/p>\n<p><strong>\/\/ Step 5 : Applying assertions to verify that the API is returning the expected data or not?<\/strong><\/p>\n<p>Assert.assertEquals(<strong>firstName<\/strong>, &#8220;Mark&#8221;);<br \/>\nAssert.assertEquals(<strong>lastName<\/strong>, &#8220;Flower&#8221;);<br \/>\nAssert.assertEquals(<strong>empAge<\/strong>, 40);<br \/>\n<strong>}<\/strong><\/p>\n<p><strong>Conclusion:\u2014<\/strong>The complexity of developing the script and running it would be significantly higher if the above-explained code were implemented without the POJO, but by utilizing the POJO, we were able to create a less complicated script. Additionally, we simplified the code&#8217;s readability and maintainability.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction:- A straightforward Java class called POJO (Plain Old Java Object) stores data object-orientedly and is independent of frameworks. POJOs simplify, tidy up, and reuse payload handling. We can improve the readability, resilience, and maintainability of our script by implementing the POJO in API automation. Java objects are automatically converted to JSON\/XML by serialization, and [&hellip;]<\/p>\n","protected":false},"author":1404,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":224},"categories":[5880],"tags":[5899,8044,5901],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/75281"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1404"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=75281"}],"version-history":[{"count":8,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/75281\/revisions"}],"predecessor-version":[{"id":76438,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/75281\/revisions\/76438"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=75281"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=75281"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=75281"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}