Home > Back-end >  Postman test of spring ArrayList getmapping response
Postman test of spring ArrayList getmapping response

Time:10-29

I have a Spring Boot application that has a getmapping returning an ArrayList. This all works fine and it returns the arraylist as a json object in the body. I am trying to write a postman test on the response to verify the length of the arraylist that comes back. I cant seem to get it to find the arraylist on the object however since there doesn't appear to be any keys.

My Spring Code is:

    @GetMapping(value="/getAllCars", produces = "application/json")
    public @ResponseBody List<Car> getAllCars() {  
        return carManager.getAllCars();
    }

The response in postman shows

[
    {
        "id": "19a94f8d-b1c1-4814-abba-57b1cb319478",
        "name": "car1"
    },
    {
        "id": "4e2568ca-f41a-480d-82b5-acede9109a6d",
        "name": "car2",
    },
    {
        "id": "4e2568ca-f41a-480d-82b5-acede9109a6d",
        "name": "car3",
    },
... (there is 72 total)

]

my test code in postman is is

pm.test("CarsLength"  , function(){
    const responseJson = pm.response.json;
    pm.expect(responseJson.length).to.equal(72);
})

when i look at the postman console after logging the responseJson and responseJson.length its only logging out the function name and 2. responseJson[0] and responseJson[1] are both undefined when i log them out as well.

I dont have a key in my response json object is thats whats causing this? How can I access the objects to verify the length in a postman test?

CodePudding user response:

The mistake is here:

pm.response.json --> pm.response.json()

  • Related