简体   繁体   English

验证 rest 保证 + java 中的多个响应主体节点(键的值)

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

Given:鉴于:

  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


    }
}

The response body json is:响应体 json 是:

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

With the above code when the last assertion is being executed, it will fail purposely, and none of the below actions will also get executed.在执行最后一个断言时,上面的代码会故意失败,并且下面的任何操作都不会被执行。 What I m trying to do is to test multiple values from the response's body, but not stop at first fail, how is this accomplished in rest assured?我要做的是测试响应正文中的多个值,但不会在第一次失败时停止,这是如何在 rest 中完成的?

++ Exception that is being raised: ++ 引发的异常:

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

There are 2 approaches for your problem.您的问题有两种方法。

Solution 1: Take advantages of assertion library has built-in Soft assertion function, such as TestNG and AssertJ .方案一:利用断言库内置Soft assertion function,如TestNGAssertJ

For example: using AssertJ例如:使用 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();
}

Solution 2: Deserialize response to POJO then compare objects.解决方案 2:反序列化对 POJO 的响应,然后比较对象。 To define how objects equal, your POJO need to override equals and hashCode method要定义对象如何相等,您的 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