簡體   English   中英

驗證 rest 保證 + java 中的多個響應主體節點(鍵的值)

[英]Validate multiple response body nodes (values for keys) in rest assured + java

鑒於:

  package com.company;

import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import org.hamcrest.core.Is;


public class Main {

    public static void main(String[] args) {
        RestAssured.baseURI = "///URL";
        RequestSpecification request = RestAssured.given();

        String body_content = "{\n" +
                "\t\"name\":\"Santosh\",\n" +
                "\t\"email\":\"santosh@SUMEMAIL.com\",\n" +
                "\t\"age\":30,\n" +
                "\t\"address\":{\n" +
                "\t\t\"city\":\"Bangalore\",\n" +
                "\t\t\"state\":\"karnataka\",\n" +
                "\t\t\"pinCode\":\"123\"\n" +
                "\t}\n" +
                "}";

//        JSONObject jsonObject = new JSONObject(body_content);

        request.header("Content-Type", "application/json");
        request.body(body_content);

        Response response = request.post("/persons");
        System.out.println("The response is \n -------------------- \n" + response.getBody().asPrettyString());

        String myPersonId = response.path("name").toString();
        System.out.println("Its my personId = " + myPersonId);
        response.then().assertThat().body("name", Is.is("Santo1sh"));
        response.then().assertThat().body("email", Is.is("not the wright email")); // Basically I want to test multiple assertions in the same execution


    }
}

響應體 json 是:

{
    "personId": "c27a3364-cd93-4c10-bb58-4a17c520b54d",
    "name": "Santosh",
    "age": 30,
    "email": "santosh@gmail.com",
    "address": {
        "city": "Bangalore",
        "state": "karnataka",
        "pinCode": 560037
    }
}

在執行最后一個斷言時,上面的代碼會故意失敗,並且下面的任何操作都不會被執行。 我要做的是測試響應正文中的多個值,但不會在第一次失敗時停止,這是如何在 rest 中完成的?

++ 引發的異常:

   Exception in thread "main" java.lang.AssertionError: 1 expectation failed.
JSON path name doesn't match.
Expected: is "Santo1sh"
  Actual: Santosh

您的問題有兩種方法。

方案一:利用斷言庫內置Soft assertion function,如TestNGAssertJ

例如:使用 AssertJ

@Test
public void test_1(){
   ...
   SoftAssertions softly = new SoftAssertions();
   softly.assertThat(response.path("name")).isEqualTo("Santo1sh");
   softly.assertThat(response.path("email")).isEqualTo("not the wright email");
   softly.assertAll();
}

解決方案 2:反序列化對 POJO 的響應,然后比較對象。 要定義對象如何相等,您的 POJO 需要覆蓋equalshashCode方法

Person expect = new Person(); 

//set value for Person fields: personId, name, email ...

Person actual = JsonPath.with(res).getObject("", Person.class);

assertThat(actual, is(expect));
import lombok.Data;
import lombok.EqualsAndHashCode;


@Data
@EqualsAndHashCode
class Person{
    private String personId;
    private String name;
    private int age;
    private String email;
    private Address address;
}

@Data
@EqualsAndHashCode
class Address{
    private String city;
    private String state;
    private long pinCode;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM