Introduction
As a part of the End to End REST Assured Tutorial, in this post, we will learn to assert a full response JSON body in Rest Assured.
If the response is kind of static or small we may want to assert the full response body directly instead of deserializing the response into some Java objects and then extract and match values.
In this post, we will learn to assert full response from an external file as well.
Dependecy Required
io.rest-assured rest-assured 4.3.3 test
Asserting full response body
We just need to use body() method with Hamcrest Matchers. I will be using GoRest APIs for example.
Example program
package RestAssuredConcepts; import org.hamcrest.Matchers; import io.restassured.RestAssured; public class AssertFullResponseBody { public static void main(String[] args) { RestAssured // Construct request .given() .log() .all() .baseUri("https://gorest.co.in/public-api/") .basePath("users/{id}") .pathParam("id", "178") // Hit API .when() .get() .then() // Assert response .log() .all() .assertThat() // Pass full expected response body with Hamcrest matchers .body(Matchers.equalTo("{\"code\":200,\"meta\":null,\"data\":{\"id\":178,\"name\":\"Puneet Rana\"," + "\"email\":\"rana_puneet@trantow.org\",\"gender\":\"Male\",\"status\":\"Inactive\"," + "\"created_at\":\"2021-02-03T03:50:07.588+05:30\",\"updated_at\":\"2021-02-03T03:50:07.588+05:30\"}}")); } }
You need to convert formatted JSON in to single line and pass in equalTo() method. If you do not convert unwanted new line and carriage return (\r\n) charctaers will be present and impact validation.
Asserting full response body from external file
We can also store expected response into an external JSON file as shown below:-
Example program
package RestAssuredConcepts; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import org.hamcrest.Matchers; import io.restassured.RestAssured; public class AssertFullResponseBodyFromExternalFile { public static void main(String[] args) throws IOException { RestAssured // Construct request .given() .log() .all() .baseUri("https://gorest.co.in/public-api/") .basePath("users/{id}") .pathParam("id", "178") // Hit API .when() .get() .then() // Assert response .log() .all() .assertThat() // Pass full expected response body with Hamcrest matchers .body(Matchers.equalTo(new String(Files.readAllBytes(Paths.get(System.getProperty("user.dir")+"\\src\\test\\resources\\jsonFiles\\UserDetails178.json"))))); } }
Note – Above expected response may change when you are referring this post. Please check expected response and make changes if required.
You can download/clone the above sample project from here.
You can subscribe to my YouTube channel RetargetCommon to learn from video tutorials.
If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappyLearning
Find all Selenium related posts here, all API manual and automation related posts here, and find frequently asked Java Programs here.
Many other topics you can navigate through the menu.