Sunday 24 November 2019

Rest Assured: Check whether field present in response or not


Using hamcrest 'hasKey' method, you can check whether a field exist in given response are not.

Example
given()
    .header("Accept", "application/json")
.when()
    .get("api/v1/employees/3")
.then()
    .body("$", hasKey("firstName"))
    .body("$", hasKey("lastName"));

TestApp.java
package com.sample.app;

import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.hasKey;

import org.junit.Test;

import io.restassured.RestAssured;

public class TestApp {

    @Test
    public void testApp() {
        RestAssured.baseURI = "http://localhost:8080";

        given().header("Accept", "application/json").when().get("api/v1/employees/3").then()
                .body("$", hasKey("firstName")).body("$", hasKey("lastName"));

    }

}




Previous                                                    Next                                                    Home

No comments:

Post a Comment