簡體   English   中英

如何使用Rest Assured將數組列表添加到Rest API請求?

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

我正在通過getter和setter方法之類的POJO類對Rest api測試使用放心的設置值,但我在rest請求之間陷入了數組列表,請任何人提供適當的代碼以准確獲取以下請求使用放心發布。

請求:

{
"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類:

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

}

付款詳情:

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

}

測試類別:

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

    }

在測試類中,您必須做的是將測試數據創建分離到另一個類中,並將其作為數據提供者傳遞給您的測試方法。 從設計的角度來看。

遇到問題:您已經在使用gson,為什么要構造一個JSONObject。 而是執行以下操作;

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

希望這是您想要的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM