You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

现有Rest Assured项目全局报错,请求协助排查

Rest Assured项目运行时全局报错问题

项目pom.xml配置

Pom.xml 配置信息

核心测试代码

package demoProject;

import io.restassured.RestAssured;
import io.restassured.path.json.JsonPath;

import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;

import org.testng.Assert;

import files.ReuseableMethods;
import files.paayload;

public class Basics {

    public static void main(String[] args) {
        // 验证添加地点API是否正常工作
        
        // Rest Assured核心流程
        // Given - 所有输入参数
        // When - 提交API请求,获取响应
        // Then - 验证响应结果
        
        RestAssured.baseURI= "https://rahulshettyacademy.com";
        String response=given().log().all().queryParam("key", "qaclick123").header("Content-Type", "application/json")
        .body(paayload.Addplace()).when().post("maps/api/place/add/json")
        .then().log().all().assertThat().statusCode(200).body("scope", equalTo("APP"))
        
        // 在then()后使用.header()表示验证响应头,此处验证服务器信息
        //.header("server", "Apache/2.4.41 (Ubuntu)");
        .header("server", "Apache/2.4.41 (Ubuntu)").extract().response().asString();
        
        System.out.println(response);
        
        JsonPath js=new JsonPath(response);  // 解析JSON响应
        
        String placeID=js.getString("place_id");
        
        System.out.println(placeID);
        // 流程:添加地点->更新地点为新地址->获取地点并验证新地址是否生效  
        
        // 任务:集成多个API,复用响应中的公共值:此处使用Place ID变量更新地点信息
        
        String newAddress = "orar, gwalior";
        given().log().all().queryParam("key", "qaclick123").header("Content-Type", "application/json").body("{\r\n"
                + "\"place_id\":\""+placeID+"\",\r\n"
                + "\"address\":\""+newAddress+"\",\r\n"
                + "\"key\":\"qaclick123\"\r\n"
                + "}").
        when().put("maps/api/place/update/json")
        .then().assertThat().log().all().statusCode(200).body("msg", equalTo("Address successfully updated"));
        
        // 获取地点信息
        
        String getPlaceResponce= given().log().all().queryParam("key", "qaclick123").queryParam("place_id", placeID)
        .when().get("maps/api/place/get/json")
        .then().log().all().statusCode(200).extract().response().asString();
        
        JsonPath js1 = ReuseableMethods.rwToJson(getPlaceResponce);
        String actualAddress= js1.get("address");
        System.out.println(actualAddress);
        Assert.assertEquals(newAddress, actualAddress);
        // 可集成cucumber, TestNG, Junit
    }

}

报错截图

报错信息截图

内容的提问来源于stack exchange,提问作者Ankit Gupta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 04:07:07