Rest Assured - 在Body中发布嵌套的POJO

时间:2016-08-04 20:42:01

标签: java json rest-assured

我尝试编写API类来发送请求并处理来自API的响应。 API的某些部分的请求需要附加到请求的JSON主体,例如此示例:

{
  "Title": "string",
  "Status": "string",
  "ActFinish": "Date",
  "ActHrs": "float",
  "ActStart": "Date",
  "ActualResults": "string",
  "AssigneeUserId": "int",
  "CustomFields": [
    {
      "Id": "string",
      "Name": "string",
      "Value": "string"
    }
  ],
  "Description": "string",
  "EstFinish": "Date",
  "EstHrs": "float",
  "EstHrsRemaining": "float",
  "EstStart": "Date",
  "ExpectedResults": "string",
  "FolderId": "int",
  "FunctionalAreaCode": "string",
  "HowFoundCode": "string",
  "IssueCode": "string",
  "ModuleCode": "string",
  "PctComplete": "int",
  "PriorityCode": "string",
  "Resolution": "string",
  "ResolutionCode": "string",
  "SeverityCode": "string",
  "SoftwareVersionCode": "string",
  "StepsToRepro": "string"
}

我通过阅读Rest Assured文档找到的最佳方法是使用此处提到的POJO:https://github.com/rest-assured/rest-assured/wiki/Usage#serialization

我的POJO看起来像这样:

public class RequestDefectPost {

    public String title;
    public String status;
    public Timestamp actFinish;
    public float actHours;
    public Timestamp actStart;
    public String actualResults;
    public int assigneeUserId;
    public String[] customFields;
    public String id;
    public String name;
    public String value;
    public String description;
    public Timestamp estFinish;
    public float estHours;
    public float estHrsRemaining;
    public Timestamp estStart;
    public String expectedResults;
    public int folderId;
    public String functionalAreaCode;
    public String howFoundCode;
    public String issueCode;
    public String moduleCode;
    public int pctComplete;
    public String priorityCode;
    public String resolutionCode;
    public String severityCode;
    public String softwareVersionCode;
    public String stepsToRepro;

    public RequestDefectPost(String title, String status) {

        this.title = title;
        this.status = status;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public Timestamp getActFinish() {
        return actFinish;
    }

    public void setActFinish(Timestamp actFinish) {
        this.actFinish = actFinish;
    }

    public float getActHours() {
        return actHours;
    }

    public void setActHours(float actHours) {
        this.actHours = actHours;
    }

    public Timestamp getActStart() {
        return actStart;
    }

    public void setActStart(Timestamp actStart) {
        this.actStart = actStart;
    }

    public String getActualResults() {
        return actualResults;
    }

    public void setActualResults(String actualResults) {
        this.actualResults = actualResults;
    }

    public int getAssigneeUserId() {
        return assigneeUserId;
    }

    public void setAssigneeUserId(int assigneeUserId) {
        this.assigneeUserId = assigneeUserId;
    }

    public String[] getCustomFields() {
        return customFields;
    }

    public void setCustomFields(String id, String name, String value) {
        this.customFields = new String[]{this.id = id, this.name = name, this.value = value};
    }

    public String getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Timestamp getEstFinish() {
        return estFinish;
    }

    public void setEstFinish(Timestamp estFinish) {
        this.estFinish = estFinish;
    }

    public float getEstHours() {
        return estHours;
    }

    public void setEstHours(float estHours) {
        this.estHours = estHours;
    }

    public float getEstHrsRemaining() {
        return estHrsRemaining;
    }

    public void setEstHrsRemaining(float estHrsRemaining) {
        this.estHrsRemaining = estHrsRemaining;
    }

    public Timestamp getEstStart() {
        return estStart;
    }

    public void setEstStart(Timestamp estStart) {
        this.estStart = estStart;
    }

    public String getExpectedResults() {
        return expectedResults;
    }

    public void setExpectedResults(String expectedResults) {
        this.expectedResults = expectedResults;
    }

    public int getFolderId() {
        return folderId;
    }

    public void setFolderId(int folderId) {
        this.folderId = folderId;
    }

    public String getFunctionalAreaCode() {
        return functionalAreaCode;
    }

    public void setFunctionalAreaCode(String functionalAreaCode) {
        this.functionalAreaCode = functionalAreaCode;
    }

    public String getHowFoundCode() {
        return howFoundCode;
    }

    public void setHowFoundCode(String howFoundCode) {
        this.howFoundCode = howFoundCode;
    }

    public String getIssueCode() {
        return issueCode;
    }

    public void setIssueCode(String issueCode) {
        this.issueCode = issueCode;
    }

    public String getModuleCode() {
        return moduleCode;
    }

    public void setModuleCode(String moduleCode) {
        this.moduleCode = moduleCode;
    }

    public int getPctComplete() {
        return pctComplete;
    }

    public void setPctComplete(int pctComplete) {
        this.pctComplete = pctComplete;
    }

    public String getPriorityCode() {
        return priorityCode;
    }

    public void setPriorityCode(String priorityCode) {
        this.priorityCode = priorityCode;
    }

    public String getResolutionCode() {
        return resolutionCode;
    }

    public void setResolutionCode(String resolutionCode) {
        this.resolutionCode = resolutionCode;
    }

    public String getSeverityCode() {
        return severityCode;
    }

    public void setSeverityCode(String severityCode) {
        this.severityCode = severityCode;
    }

    public String getSoftwareVersionCode() {
        return softwareVersionCode;
    }

    public void setSoftwareVersionCode(String softwareVersionCode) {
        this.softwareVersionCode = softwareVersionCode;
    }

    public String getStepsToRepro() {
        return stepsToRepro;
    }

    public void setStepsToRepro(String stepsToRepro) {
        this.stepsToRepro = stepsToRepro;
    }
}

使用我当前的POJO,Rest Assured吐出的JSON如下所示:

{
  "title": "Test",
  "status": "New",
  "actFinish": null,
  "actHours": 0.0,
  "actStart": null,
  "actualResults": null,
  "assigneeUserId": 0,
  "customFields": [
    "Test",
    "test",
    "tesT"
  ],
  "id": "Test",
  "name": "test",
  "value": "tesT",
  "description": null,
  "estFinish": null,
  "estHours": 0.0,
  "estHrsRemaining": 0.0,
  "estStart": null,
  "expectedResults": null,
  "folderId": 0,
  "functionalAreaCode": null,
  "howFoundCode": null,
  "issueCode": null,
  "moduleCode": null,
  "pctComplete": 0,
  "priorityCode": null,
  "resolutionCode": null,
  "severityCode": null,
  "softwareVersionCode": null,
  "stepsToRepro": null
}

我的问题是如何编写customFields以使其正确嵌套,如示例JSON中所述?

1 个答案:

答案 0 :(得分:0)

您需要另一个代表" CustomFields"的POJO。例如:

public class CustomFields {
     private String Id;
     private String Name;
     private String Value;

     <getters and setters>
}

并使用List<CustomFields> CustomFields代替String[] customFields。另请注意,您的JSON示例并未对属性名称使用驼峰大小写,因此您的POJO也不应使用驼峰大小写。

有很多方法可以避免在REST Assured中使用POJO。例如,您可以使用HashMap代替在某些情况下更容易。