繁体   English   中英

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

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

我是 TestNG 的新手,我正在尝试测试响应主体是否有数据。 现在,如果我在 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
            ]
        }, ... ]

现在我应该使用 TestNG 测试相同的http://localhost:8080/sportsbetting-web/loadEvents API 端点,但我应该怎么做呢? 我试过这个:

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

它返回 200 OK 响应,但是我想测试响应正文是否包含 JSON 数据,例如id, title

最好的选择是创建响应的对象表示,例如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;
    }
}

测试看起来像:

@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());
        }
    }
}

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

笔记:

  1. 其他依赖项(在下面的示例中,我使用的是 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. 而不是在MyEvent类中编写多个 getter/setter - 最好使用 Lombok 库(google it),只需在MyEvent类下添加@Getter@Setter注释。 这是将 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