Mate25
Mate25

Reputation: 65

How to check if response body has data?

Im new to TestNG and I'm trying to test if the respone body has data. Right now, the JSON body gives back these datas, if I run the http://localhost:8080/sportsbetting-web/loadEvents on POSTMAN.

   [ {
            "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? 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

Upvotes: 1

Views: 1179

Answers (1)

Andrey Kotov
Andrey Kotov

Reputation: 1405

Best option will be is to create object representation of your response, like MyEvent.java. For fast-creating such classes use online tools like this.

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: enter image description here

Notes:

  1. Additional dependencies (in example below I am using gradle syntax, but same idea will be applied for 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. This is how Lombok can be added to gradle project:

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

Upvotes: 1

Related Questions