如何在可靠的API测试中传递数组发布请求?

时间:2017-08-01 16:41:14

标签: java rest-assured

我是API测试的新手,并试图找出如何通过单个数组传递post请求主体,该数组在重新保证的API测试中包含多个请求和属性集合。

{
    "Transactions":
    [ 
      {"ReferenceId":"01","Id":"0727", "TCID": "67180405816294"},
      {"ReferenceId":"02","Id":"0727", "TCID": "67180405816294"},
      {"ReferenceId":"03","Id":"0727", "TCID": "67180405816294"}

    ]
}

1 个答案:

答案 0 :(得分:1)

听起来您希望使用restassured将特定对象发布为帖子请求的正文。类似下面的内容应该有效:

// If you are using Object Mapping (e.g. GSON or Jackson) create your test data as java objects
List<Reference> references = ...;
TransactionDTO data = new TransactionDTO(references);

// Else, not using mapping, so create test data as string:
String data = "{ \"Transactions\": [ ...]}";

given()
  .contentType("application/json")
  .body(data)
  .queryParam("key", "value") //omit if not needed
when()
  .post("/post/url/path")
then()
  .<whatever assertions you need to make>

参考:https://github.com/rest-assured/rest-assured/wiki/Usage