0

In Rest Assured Framework POST response is not showing ID created and Time that I am getting through POSTMAN.

@Test
public void newuser() {

    service = new services();

    // response = service.addProduct("444", "OIL","Natural Tea Tree", "210.0");

    response = service.AddUser("Gagan", "leeee");

    if (response.statusCode() == 201) {

        Gson gson = new Gson();

        System.out.println(response.asString());

    PostUser[] userlist = gson.fromJson(response.asString(), PostUser[].class);

    System.out.println(userlist[0].getCreatedAt());


        System.out.println(response.statusCode());

    }
}

}

Eclipse Console Output-----

[{"name":"Gagan","job":"leeee"}]
null
201

Postman Output

{
    "name": "Gagan",
    "job": "leee",
    "id": "327",
    "createdAt": "2019-02-08T07:42:50.664Z"
}

PostUser Class

package webservices.responsePOJO;

import com.google.gson.annotations.Expose; import com.google.gson.annotations.SerializedName;

public class PostUser {

    @SerializedName("name")
    @Expose
    private String name;
    @SerializedName("job")
    @Expose
    private String job;
    @SerializedName("id")
    @Expose
    private String id;
    @SerializedName("createdAt")
    @Expose
    private String createdAt;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(String createdAt) {
        this.createdAt = createdAt;
    }

}
  • You are passing 2 parameters to `service.AddUser()`, and the response has those 2 fields in it. Are `id` and `createdAt` supposed to be auto generated? – Andy Sug Feb 08 '19 at 08:01
  • yes they are auto generated through POST API – Gagandeep Singh Feb 08 '19 at 09:57
  • This is not the right approach to [rest-assured], anyways to start with pls post the class `PostUser`, I do believe that your response does have the body with id and createdAt son keys, however this is not transferred to `PostUser` by the json to object deserialisation. – SudhirR Feb 21 '19 at 10:28
  • Better use of the [rest-assured] framework will be to use `response.then().root("[0]").body("name", equalTo("Gagan"))...` See if this works for you. – SudhirR Feb 21 '19 at 10:32
  • I do have Id and created at son keys – Gagandeep Singh Feb 28 '19 at 10:14

0 Answers0