如何在 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