如何在 Rest 保證驗證響應中納入 TestNG 斷言?
我們可以在 Rest 保證驗證響應中納入 TestNG 斷言。為了使用 TestNG,我們必須在 Maven 專案中的 pom .xml 中新增以下依賴關係。此依賴關係的連結可在以下連結中獲取 −
https://mvnrepository.com/artifact/org.testng/testng

要使用 TestNG 斷言驗證響應,我們需要使用 Assert 類的方法。我們將首先在模擬 API URL 上透過 Postman 傳送 GET 請求,並瀏覽響應。

示例
使用 Rest 保證和 TestNG,我們將驗證課程欄位的值——自動化測試。
程式碼實現
import org.testng.Assert;
import org.testng.annotations.Test;
import static io.restassured.RestAssured.*;
import io.restassured.RestAssured;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
import io.restassured.response.ResponseBody;
import io.restassured.specification.RequestSpecification;
public class NewTest {
@Test
void verifyTestNg() {
String c = "Automation Testing";
//base URI with Rest Assured class
RestAssured.baseURI = "https://run.mocky.io/v3";
//input details
RequestSpecification h = RestAssured.given();
//get response
Response r = h.get("/e3f5da9c-6692-48c5-8dfe-9c3348cfd5c7");
//Response body
ResponseBody bdy = r.getBody();
//convert response body to string
String b = bdy.asString();
//JSON Representation from Response Body
JsonPath j = r.jsonPath();
//Get value of Location Key
String l = j.get("Course");
System.out.println("Course name: " + l);
Assert.assertEquals(l, c);
}
}輸出

廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP