简体   繁体   English

验证所有响应数据的最佳方法是什么?

[英]What is the best way to verify the all response data in restassured?

I have a response body as below.我有一个响应主体如下。 So i want to verify all this response.所以我想验证所有这些响应。 But this response values can change.但是这个响应值可以改变。 I think i need to regex and parse json right?我想我需要正则表达式和解析 json 对吗? How can i write this?我怎么写这个?

 {
    "Quota-Bytes": "5368709120",
    "Object-Count": "142",
    "Bytes-Used": "12510163",
    "totalUsage": 12510163,
    "imageUsage": 12510163,
    "videoUsage": 0,
    "audioUsage": 0,
    "othersUsage": 0,
    "totalFileCount": 142,
    "folderCount": 14,
    "imageCount": 142,
    "videoCount": 0,
    "audioCount": 0,
    "othersCount": 0
 }

Thank you谢谢

The easiest way is to cast your response to a map and match to some predefined map (expected results)最简单的方法是将您的响应投射到 map 并匹配一些预定义的 map(预期结果)

package so.http;

import io.restassured.RestAssured;
import java.util.Map;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;

public class RAValidation {

    static final Map expected = Map.ofEntries(
            Map.entry("Quota-Bytes", "5368709120"),
            Map.entry("Object-Count", "142"),
            Map.entry("Bytes-Used", "12510163"),
            Map.entry("totalUsage", 12510163d),
            Map.entry("imageUsage", 12510163d),
            Map.entry("videoUsage", 0d),
            Map.entry("audioUsage", 0d),
            Map.entry("othersUsage", 0d),
            Map.entry("totalFileCount", 142d),
            Map.entry("folderCount", 14d),
            Map.entry("imageCount", 142d),
            Map.entry("videoCount", 0d),
            Map.entry("audioCount", 0d),
            Map.entry("othersCount", 0d)
    );

    public static void main(String[] args) {
        Map<String, Integer> observed = RestAssured
                .get("http://demo1954881.mockable.io/")
                .as(Map.class);
        assertThat(observed.entrySet(), equalTo(expected.entrySet()));
    }
    
}

Note that JSON number is not segregated by subtypes so it is generally represented by a floating point.请注意,JSON 数字不按子类型分隔,因此通常用浮点数表示。 So that when you prepare numbers for expected set, convert them to doubles explicilty.因此,当您为expected集合准备数字时,将它们转换为doubles数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将所有动态 Json 响应字符串存储到放心的 java 列表中的正确方法是什么? - What is the correct way to store all the dynamic Json response strings to a list in restassured java? RestAssured - 想在 RestAssured 中验证 JSON 响应的主体结构 - RestAssured - want to verify the body structure of JSON response in RestAssured 在所有活动中共享数据的最佳方式是什么? - What is the best way to share data throughout all activites? restAssured发送请求的两种方式有什么区别? - what is the difference in the two way of sending requests by restAssured? RestAssured:验证实体的创建 - RestAssured: verify creation of entity 验证多个jcombobox项选择的最佳方法是什么? - What is the best way to verify multiple jcombobox item selection? 没有HATEOAS,获得响应的最佳方法是什么? - What is the best way to get response without HATEOAS? 编写可处理大型Web应用程序中响应内容类型的所有特殊情况的通用httpservletresponsewrapper的最佳方法是什么? - What is the best way to write a generic httpservletresponsewrapper that handles all special cases of response's contenttype in a large web app? 用Java处理GraphQl响应的最佳方法是什么 - What is the best way to handle GraphQl response in Java 如何解析和获取一些,验证来自再保证 api 和 Java 的响应中的值 - How to parse and get some,verify values from response in restassured api with Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM