简体   繁体   中英

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? 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)

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. So that when you prepare numbers for expected set, convert them to doubles explicilty.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM