Reputation: 87
I am using GSON to serialise and deserialise JSON response while supplying as payload and mapping response to the data model.
Now, here id is auto-incremented from DB so we don't need to pass when creating payload.
JSON payload: (updateCustomer) {"first_name":"test", "last_name":"user"}
public class Address {
@SerializedName("id")
private Integer id;
@SerializedName("first_name")
private String firstname;
@SerializedName("last_name")
private String lastname;
....
}
Test:
Response response =
given()
.filter(new RequestLoggingFilter(this.requestCapture))
.filter(new ResponseLoggingFilter(this.responseCapture))
.filter(new ErrorLoggingFilter(this.errorCapture))
.header("Authorization", getSession().getToken())
.body(updateCustomer)
.when()
.put(Resource.UPDATE_CUSTOMER)
.then()
.extract().response();
Expected Response in response instance {"id":2234545, "first_name":"test", "last_name":"user"}
Response.toString() returns io.restassured.internal.RestAssuredResponseImpl@46320c9a instead of Response body string.
I've tried response.body().toString(),
@Expose(deserialize = false)
@SerializedName("id")
private Integer id;
but no luck.
Expecting the response body as the string so that I can map using GSON to Java objects to perform the validation but getting io.restassured.internal.RestAssuredResponseImpl@46320c9a
I appreciate if someone can please direct me on this issue.
Many thanks,
Upvotes: 6
Views: 16072
Reputation: 1236
@Dipesh
Instead of response.body().toString();
try response.getBody().asString();
See the sample code I have done below and the output
Code
package com.restassured.framework.sample;
import static io.restassured.RestAssured.given;
import org.testng.annotations.Test;
import io.restassured.response.Response;
/**
* @author vamsiravi
*
*/
public class RestAssuredExample {
@Test
public void sampleTest(){
Response response = given().baseUri("https://jsonplaceholder.typicode.com/").and().basePath("/posts/1").when().get().thenReturn();
System.out.println(response.body());
System.out.println("---------------------------");
System.out.println(response.getBody().asString());
}
}
Output
io.restassured.internal.RestAssuredResponseImpl@652a7737
---------------------------
{
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
}
Upvotes: 15
Reputation: 436
given()
.filter(new RequestLoggingFilter(this.requestCapture))
.filter(new ResponseLoggingFilter(this.responseCapture))
.filter(new ErrorLoggingFilter(this.errorCapture))
.header("Authorization", getSession().getToken())
.body(updateCustomer)
.when()
.put(Resource.UPDATE_CUSTOMER)
.then()
.body("id", equalTo("2234545"));
Hamcrest matcher import:
import static org.hamcrest.core.IsEqual.equalTo;
Upvotes: 0