首页 > 解决方案 > RestAssured 断言失败

问题描述

功能文件片段:

那么messages.type的值应该是ERROR

实际服务响应:“消息”:[{“类型”:“错误”}]

控制台日志:

JSON 路径 messages.type 不匹配。预期:包含“ERROR”的字符串实际:[ERROR]

我尝试从功能文件中提到的 ERROR 参数中删除双引号,但它不起作用

标签: cucumberrest-assuredrest-assured-jsonpathfeature-file

解决方案


因为您没有提供您使用的代码,这可能是因为您将 json 响应转换为 String 。请尝试以下代码,因为它有一个如何将 Json 转换为 String 的示例。

public void jsonPathExample() {
        Response response=given().contentType(ContentType.JSON).get("http://localhost:3000/posts");
          //we need to convert response as a String 
        JsonPath jsonPath = new JsonPath(response.asString());
        String actualtype = jsonPath.getString("type");
//Then do your assertion
    }

推荐阅读