- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to verify a JSON response body using Assertions in Rest Assured?
We can verify the JSON response body using Assertions in Rest Assured. This is done with the help of the Hamcrest Assertion. It uses the Matcher class for Assertion.
To work with Hamcrest we have to add the Hamcrest Core dependency in the pom.xml in our Maven project. The link to this dependency is available in the below link −
https://mvnrepository.com/artifact/org.hamcrest/hamcrest-core
We shall send a GET request via Postman on a mock API, observe the Response.
Using Rest Assured, we shall verify the value of the Location in the Response body.
Code Implementation
import org.hamcrest.Matchers; import org.testng.annotations.Test; import static io.restassured.RestAssured.given; import io.restassured.RestAssured; public class NewTest { @Test public void ressponseAssertion() { //base URL RestAssured.baseURI = "https://run.mocky.io"; //GET operation given() .when().get("/v3/6c6ed634-5e78-4b80-94c7-cf17c04c7055"). then().log().all() //verify status code as 200 .assertThat().statusCode(200) //verify body .body("Location", Matchers.equalTo("Makinac Island")) //verify header .header("Content-Length" , "57"); } }
Output
Advertisements