简体   繁体   中英

How to add array list to rest API request using Rest Assured?

I'm using rest assured for Rest api testing with the help of POJO classes like getter and setter methods to set the values but i'm stuck with array list in between rest request,please any one provide proper code to get exact below request to post using rest assured.

Request:

{
"firstName":"SuryaNAMASKARAM",
"lastName":"mangalam",
"mobileNo" :4954758490,
"emailId" :"surya.mangalam@futureretail.in",
"houseNoStreet":"123456",
"buildingName":"",
"landmark":"Nirmala jathara",
"paymentDetail" :
[{"paymentType":"CASH","No":"3519000012","Date":"16-06-2018","amount":"100.00"}]
}

CustomerCreate Class:

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Example {

@SerializedName("firstName")
@Expose
private String firstName;
@SerializedName("lastName")
@Expose
private String lastName;
@SerializedName("mobileNo")
@Expose
private Integer mobileNo;
@SerializedName("emailId")
@Expose
private String emailId;
@SerializedName("houseNoStreet")
@Expose
private String houseNoStreet;
@SerializedName("buildingName")
@Expose
private String buildingName;
@SerializedName("landmark")
@Expose
private String landmark;
@SerializedName("paymentDetail")
@Expose
private List<PaymentDetail> paymentDetail = null;

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public Integer getMobileNo() {
return mobileNo;
}

public void setMobileNo(Integer mobileNo) {
this.mobileNo = mobileNo;
}

public String getEmailId() {
return emailId;
}

public void setEmailId(String emailId) {
this.emailId = emailId;
}

public String getHouseNoStreet() {
return houseNoStreet;
}

public void setHouseNoStreet(String houseNoStreet) {
this.houseNoStreet = houseNoStreet;
}

public String getBuildingName() {
return buildingName;
}

public void setBuildingName(String buildingName) {
this.buildingName = buildingName;
}

public String getLandmark() {
return landmark;
}

public void setLandmark(String landmark) {
this.landmark = landmark;
}

public List<PaymentDetail> getPaymentDetail() {
return paymentDetail;
}

public void setPaymentDetail(List<PaymentDetail> paymentDetail) {
this.paymentDetail = paymentDetail;
}

}

PaymentDetails:

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

public class PaymentDetail {

@SerializedName("paymentType")
@Expose
private String paymentType;
@SerializedName("No")
@Expose
private String no;
@SerializedName("Date")
@Expose
private String date;
@SerializedName("amount")
@Expose
private String amount;

public String getPaymentType() {
return paymentType;
}

public void setPaymentType(String paymentType) {
this.paymentType = paymentType;
}

public String getNo() {
return no;
}

public void setNo(String no) {
this.no = no;
}

public String getDate() {
return date;
}

public void setDate(String date) {
this.date = date;
}

public String getAmount() {
return amount;
}

public void setAmount(String amount) {
this.amount = amount;
}

}

Test Class:

import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.test.requestpojo.Example;
import com.test.requestpojo.PaymentDetail;

public class TestAPI {

    public void setTestData() throws JSONException {

        Example example = new Example();

        example.setFirstName("Rajesh");
        example.setLastName("Kuchana");
        example.setMobileNo("3434343434");
        example.setEmailId("rajesh.kuchana@futureretail.in");
        example.setDateOfBirth("10-10-2018");
        example.setGender(1);
        example.setHouseNoStreet("Test");
        example.setBuildingName("Test");
        example.setLandmark("Test");        

        List<PaymentDetail> data = new ArrayList<PaymentDetail>();

        PaymentDetail paymentDetail = new PaymentDetail();
        paymentDetail.setAmount("999.00");
        data.add(paymentDetail);            

        JSONObject jsonObject = new JSONObject(example);

        JSONArray jsonArray = new JSONArray(data);
        jsonArray.put(data);

        System.out.println(jsonArray.put(data));        

    }

In your test class, what you must do is separate the test data creation to a different class and pass that as data provider to your test method. This is from design point of view.

Coming to your problem: You are already using gson, why are you constructing a JSONObject. Instead do something like below;

Gson gson = new Gson(); 
String _my_obj = gson.toJson(example);

Hope this is what you are looking for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM