How to use the then method in Rest Assured?


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

Updated on: 22-Nov-2021

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements