How to use the then method in Rest Assured?

Rest AssuredDynamic ProgrammingProgramming

We can use the then method in Rest Assured. It is mainly used to validate a Response obtained from a request. Thus, most assertions are included within a then method.

Syntax

RestAssured.baseURI = "http://dummy.restapiexample.com";

//GET operation with then methods
given()
.when().get("/api/v1/employees").then()

//verify status code as 404
.assertThat().statusCode(404);

Example

Code Implementation

import org.testng.annotations.Test;
import static io.restassured.RestAssured.*;
import io.restassured.RestAssured;
public class NewTest {
   @Test
   void test() {

      //base URL
      RestAssured.baseURI =
      "http://dummy.restapiexample.com";

      //input details for GET request
      given()
      .when().get("/api/v1/employee/1")

      //verify status code as 200 within then method
      .then().log().all()
      .assertThat().statusCode(200);
   }
}

Output

raja
Updated on 22-Nov-2021 09:54:20

Advertisements