简体   繁体   English

如何检查响应正文是否有数据?

[英]How to check if response body has data?

Im new to TestNG and I'm trying to test if the respone body has data.我是 TestNG 的新手,我正在尝试测试响应主体是否有数据。 Right now, the JSON body gives back these datas, if I run the http://localhost:8080/sportsbetting-web/loadEvents on POSTMAN .现在,如果我在 POSTMAN 上运行http://localhost:8080/sportsbetting-web/loadEvents POSTMAN JSON 正文会返回这些数据。

   [ {
            "id": 2,
            "title": "Fradi vs UTE",
            "type": "Football Match",
            "start": [
                2022,
                5,
                29,
                8,
                47,
                54,
                383000000
            ],
            "end": [
                2022,
                5,
                29,
                10,
                47,
                54,
                383000000
            ]
        }, ... ]

Now I should test the same http://localhost:8080/sportsbetting-web/loadEvents API endpoint with TestNG, but how should I do it?现在我应该使用 TestNG 测试相同的http://localhost:8080/sportsbetting-web/loadEvents API 端点,但我应该怎么做呢? I tried this:我试过这个:

@Test
public void testGetEvents(){
    given().when().get("http://localhost:8080/sportsbetting-web/loadEvents").then().statusCode(200);
}

It gives back 200 OK response, however I would like to test if the response body contains JSON data, such as id, title它返回 200 OK 响应,但是我想测试响应正文是否包含 JSON 数据,例如id, title

Best option will be is to create object representation of your response, like MyEvent.java .最好的选择是创建响应的对象表示,例如MyEvent.java For fast-creating such classes use online tools like this .要快速创建此类课程,请使用此类在线工具。

MyEvent.java : MyEvent.java

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import java.util.List;

@JsonIgnoreProperties(ignoreUnknown = true)
public class MyEvent {
    private Integer id;
    private String title;
    private String type;
    private List<Integer> start;
    private List<Integer> end;

    public Integer getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public List<Integer> getStart() {
        return start;
    }

    public void setStart(List<Integer> start) {
        this.start = start;
    }

    public List<Integer> getEnd() {
        return end;
    }

    public void setEnd(List<Integer> end) {
        this.end = end;
    }
}

And test will look like:测试看起来像:

@Test
public void testGetEvents() {
    /*
        I had to use online service to simulate your response, so please uncomment the line:
        List<MyEvent> events = given().when().get("http://localhost:8080/sportsbetting-web/loadEvents")
    */
    List<MyEvent> events = given().when().get("https://sportsbetting.free.beeceptor.com/my/api/path")
            .then().statusCode(200)
            .extract().as(new TypeRef<List<MyEvent>>() {});

    System.out.println("Total amount of events: " + events.size());

    System.out.println("For each event - show if 'id' or 'title' exist");
    int eventCounter = 0;
    for (MyEvent event : events) {
        System.out.println("Processing event number " + (++eventCounter));
        if (event.getId() != null) {
            System.out.println("Id " + event.getId());
        }
        if (event.getTitle() != null) {
            System.out.println("Title " + event.getTitle());
        }
    }
}

Result of test execution:测试执行结果: 在此处输入图像描述

Notes:笔记:

  1. Additional dependencies (in example below I am using gradle syntax, but same idea will be applied for maven):其他依赖项(在下面的示例中,我使用的是 gradle 语法,但同样的想法也适用于 maven):

     implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.13.3' implementation group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.13.3' implementation group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.13.3'
  2. Instead of writing multiple getters/setters in MyEvent class - better use Lombok library (google it), and just add @Getter and @Setter annotations right under MyEvent class.而不是在MyEvent类中编写多个 getter/setter - 最好使用 Lombok 库(google it),只需在MyEvent类下添加@Getter@Setter注释。 This is how Lombok can be added to gradle project:这是将 Lombok 添加到 gradle 项目的方式:

     compileOnly 'org.projectlombok:lombok:1.18.24' annotationProcessor 'org.projectlombok:lombok:1.18.24'

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

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