如何在 Rest Assured 中使用 then 方法?


我們可以在 Rest Assured 中使用 then 方法。它主要用於驗證從請求中獲得的響應。因此,大多數斷言都包含在 then 方法中。

語法

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);

示例

程式碼實現

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);
   }
}

輸出

更新時間:2021 年 11 月 22 日

1 千次+ 瀏覽量

開啟你的職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.