Wasim
Wasim

Reputation: 61

RestAssured: How to validate response body with JSON schema if response body has extra values?

I am validating json schema using matchesJsonSchemaInClasspath. It is working fine if response body have the same values that are defined in schema.json file.
If response body have EXTRA variable / value which is not define in json schema then it does not fail. How to fail this test case?

FOR EXAMPLE:
Below is response body which has predefined JSON schema.

 {  
    "employee": {  
        "name":       "sonoo",   
        "salary":      56000,   
        "married":    true  
    }  
}  

If response body gives extra values such as email / phone then it is still passing. I need to make it fail.This is my test case to fail if response body return extra value. How to validate this test case?

{  
    "employee": {  
        "name":       "Mike",   
        "salary":      56000,  
        "Phone": "+XXX",
        "email": "[email protected]",
        "married":    true  
    }  
}  

Upvotes: 0

Views: 4416

Answers (1)

user2391218
user2391218

Reputation: 412

Create a POJO class representing the json

public class Employee {
 private String name;
 private float salary;
 private boolean married;


 // Getter Methods 

 public String getName() {
  return name;
 }

 public float getSalary() {
  return salary;
 }

 public boolean getMarried() {
  return married;
 }

 // Setter Methods 

 public void setName(String name) {
  this.name = name;
 }

 public void setSalary(float salary) {
  this.salary = salary;
 }

 public void setMarried(boolean married) {
  this.married = married;
 }
}

Use the following rest assured command to deserialize the response

Employee emp = response.getBody().as(Employee.class);

The above command will automatically fail and throw an error when additional field such as email or phone number is added to response body.

Upvotes: 2

Related Questions