[英]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.