简体   繁体   English

如何使用Rest Assured将数组列表添加到Rest API请求?

[英]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. 我正在通过getter和setter方法之类的POJO类对Rest api测试使用放心的设置值,但我在rest请求之间陷入了数组列表,请任何人提供适当的代码以准确获取以下请求使用放心发布。

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: CustomerCreate类:

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. 遇到问题:您已经在使用gson,为什么要构造一个JSONObject。 Instead do something like below; 而是执行以下操作;

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

Hope this is what you are looking for. 希望这是您想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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