Response.toString()返回io.restassured.internal.RestAssuredResponseImpl@46320c9a而不是响应正文字符串

时间:2018-06-28 19:11:11

标签: gson rest-assured

我正在使用GSON对JSON响应进行序列化和反序列化,同时将其作为有效载荷和映射响应提供给数据模型。

现在,这里的id是从数据库自动增加的,因此我们在创建有效负载时无需传递。

JSON有效负载:(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;

 ....

}

测试:

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

响应实例中的预期响应 {“ id”:2234545,“ first_name”:“测试”,“ last_name”:“用户”}

Response.toString()返回io.restassured.internal.RestAssuredResponseImpl@46320c9a而不是响应正文字符串。

我尝试了response.body()。toString(),

@Expose(deserialize = false)
@SerializedName("id")
private Integer id; 

但没有运气。

将响应主体作为字符串,以便我可以使用GSON映射到Java对象以执行验证,但得到 io.restassured.internal.RestAssuredResponseImpl@46320c9a

我很高兴有人可以指导我解决这个问题。

非常感谢,

2 个答案:

答案 0 :(得分:2)

@Dipesh

尝试使用response.body().toString();代替response.getBody().asString();

请参阅下面我完成的示例代码和输出

代码

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

}

输出

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"
}

答案 1 :(得分:0)

            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匹配器导入:

import static org.hamcrest.core.IsEqual.equalTo;