0

I am trying to test two Rest API Endpoints , one is a get and another is a post. The idea is that the response body returned by the get response shall be the body of the post.

Code that I am using:

RestAssured.baseURI = ROOT_URI;
RequestSpecification httpRequest = 
RestAssured.given().header("Authorization", "Basic " + encodedString);
String endPoint="/v1/getworkers";
Response response = httpRequest.request(Method.GET, endPoint);
String responseBody = response.getBody().asString();

Now the response body that I am getting is :

{"workerDetails":
    [
    {"workerId":"TEST123456",
    "securityId":"TESTWORKERID",
    "workerStatus":"Active",
    "firstName":"Test",
    "lastName":"Userone"}
    ]
}

Now, I do the post request and put the String responseBody as the body:

RestAssured.baseURI = ROOT_URI;
String endpoint="/v1/PostWorkers";
RequestSpecification httpRequest = 
RestAssured.given().header("Authorization", "Basic " + encodedString)
            .header("Content-Type", "application/json").body(postbody);
Response response = httpRequest.request(Method.POST,endpoint);
String responseBody = response.getBody().asString();

And I am getting the error 400 :

timestamp":1552670194453,"status":400,"error":"Bad Request","exception":"org.springframework.http.converter.HttpMessageNotReadableException","message":"JSON parse error: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token\n at [Source: java.io.PushbackInputStream@56bba771

What to do ?

Dherik
  • 17,757
  • 11
  • 115
  • 164

1 Answers1

1

The issue according to the error log is:

JSON parse error: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token

It means that the API is expecting an ArrayList as input, but is getting an Object as input instead. In other words, it would be expecting an input with ArrayList<WorkerDetail>, but you're passing an input of object WorkerDetails, which contains a field workerDetails of type ArrayList<WorkerDetail>.

Please try modifying the response of the get request from:

String responseBody = response.getBody().asString();

to:

List responseBody = response.jsonPath().getList("workerDetails");

Then pass this responseBody to the body() method on the POST request.

Madhu Bhat
  • 13,559
  • 2
  • 38
  • 54