Возникла проблема при тестировании метода post с вложенными тегами в Java и RestAsured - PullRequest
0 голосов
/ 07 июня 2018
RequestSpecification request = RestAssured.given().header("Content-Type", "application/json");
    JSONObject requestParams = new JSONObject();
    JSONObject childObject1= new JSONObject();
    JSONObject childObject2 = new JSONObject();
    requestParams.put("contactClass", "ZWSS"); 
    requestParams.put("contactActivity", "0039");
    requestParams.put("contractAccountNumber", "210024144291");
    requestParams.put("text", "Please rate the overall ease of using the website to initiate or make your service request");
    requestParams.put("contactType", "Z1");
    requestParams.put("contactDirection", "1");
    childObject1.put("question", "0001");
    childObject1.put("answer", "01");
    childObject1.put("question", "0002");
    childObject1.put("answer", "02");

    requestParams.put("addInfo", childObject1);
    requestParams.put("addInfo", childObject2);

    request.body(requestParams.toString());
    Response response = request.post("https://api-dev.adp.com/api/*/*/*");

Я пытаюсь протестировать метод Post с вложенными тегами в среде Restassured, и выше приведен фрагмент кода, в котором я строю запрос в объекте JSON.По некоторым причинам мой JSON не содержит оба значения (Вопросы и Ответы) Addinfo, и запрос не выполняется, и Запрос выполняется в следующем формате.

{"contractAccountNumber":"210024144291",
"contactClass":"ZWSS",
"contactActivity":"0039",
"contactType":"Z1",
"addInfo":{"question":"0002","answer":"02"},
"text":"Please rate the overall ease of using the website to initiate or 
make your service request",
"contactDirection":"1"}

Но Запрос должен быть в следующем формате

 {
"contactClass": "ZWSS",
"contactActivity": "0039",
"contractAccountNumber": "210024144291",
"text": "Please rate the overall ease of using the website to initiate or 
make your service request",
"contactType": "Z1",
"contactDirection": "1",
"addInfo": [{
"question": "0001",
"answer": "01"
},
{
"question": "0002",
"answer": "02"
}
]
}

Любой может помочь мне исправить это ..

1 Ответ

0 голосов
/ 07 июня 2018

Вы должны передать addInfo как массив

    childObject1.put("question", "0001");
    childObject1.put("answer", "01");

    childObject2.put("question", "0002");
    childObject2.put("answer", "02");

    JSONArray jsonArray = new JSONArray();

    jsonArray.put(childObject1);
    jsonArray.put(childObject2);

    requestParams.put("addInfo", jsonArray);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...