How to POST the below request using RestAssured in selenium.
The request is as follows:
{
"ShipmentID": "",
"ShipmentNumber": "123455-6",
"Comments": "",
"LineIDs": [
{
"ShipmentDID": "",
"AssetNum": "759585",
"FileC": "",
"SerialN": "",
"LineID": "5",
"Status": "Accept",
"TransferCancelComment": ""
}
Below is the code I have used but not sure how should i continue for the "LineID's" as it has few more attributes in it.
@Test
public void TransferIn() {
RestAssured.baseURI="testurl.rest.com";
RequestSpecification httpRequest = RestAssured.given();
JSONObject requestparams=new JSONObject();
try {
requestparams.put("ShipmentID", "");
requestparams.put("ShipmentNumber", "123455-6");
requestparams.put("Comments", "");
requestparams.put("LineIDs", "");
}
A better approach could be, construct the json from a POJO/model file and then pass that to the test. By this, there is clear separation of the intent and in future if you want to verify any response of that type, you can simply de-serialize and get the values using getters of the POJO.
Hope below code will solve your problem.
e.g if your json is
Your POJO would look something like below:
I am using GSON from google which is serialization and de-serialization library. Construct your payload using your POJO and pass that as an argument to your test method. This will make your code more readable, maintainable, scalable ....
The idea behind this was the intent of test should not be polluted and there will be clear separation between the responsibilities of different entities.