问题
The below code when executed
public void RegistrationSuccessful()
{
RestAssured.baseURI ="http://restapi.demoqa.com/customer";
RequestSpecification request = RestAssured.given();
JSONObject requestParams = new JSONObject();
requestParams.put("FirstName", "Virender"); // Cast
requestParams.put("LastName", "Singh");
request.body(requestParams.toJSONString());
Response response = request.post("/register");
}
returns
{
"FirstName": "Virender",
"LastName": "Singh"
}
Can someone please guide the rest assured code for the below JSON ?
{
"FirstName": "Virender",
"LastName": "Singh",
"Address": {
"Line1": "flat no 101",
"area": "andheri",
"City": "Mumbai"
}
}
回答1:
You can use JSONObject, HashMap, or a POJO for this
A sample code using JSONObject, I haven't tested the below code so let me know if it doesn't work
JSONObject requestparams = new JSONObject();
JSONArray authArray = new JSONArray();
JSONObject authparam = new JSONObject();
requestParams.put("FirstName", "Virender");
requestParams.put("LastName", "Singh");
authparam.put("Line1", "Flat no 101");
authparam.put("Area", "Andheri");
authparam.put("City", "Mumbai");
authArray.add(authparam);
requestparams.put("Address", authparam);
req.body(requestparams.toJSONString());
Response response = req.post("http://restapi.demoqa.com/customer/register");
Also a sample using HashMap
Map<String, Object> map = new HashMap<>();
map.put("FirstName", "Virender");
map.put("LastName", "Singh");
map.put("Address", Arrays.asList(new HashMap<String, Object>() {{
put("Line1", "Flat no 101");
put("Area", "Andheri");
put("City", "Mumbai");
}}
));
RequestSpecification req=RestAssured.given();
req.header("Content-Type","application/json");
req.body(map).when();
Response resp = req.post("http://restapi.demoqa.com/customer/register");
回答2:
The practical way to handle nested json is to serialize the json by a POJO. The POJO for the given json would be: [Here I am using gson]
public class Address {
@SerializedName("Line1")
@Expose
private String line1;
@SerializedName("area")
@Expose
private String area;
@SerializedName("City")
@Expose
private String city;
public String getLine1() {
return line1;
}
public void setLine1(String line1) {
this.line1 = line1;
}
public String getArea() {
return area;
}
public void setArea(String area) {
this.area = area;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
-----------------------------------com.example.Example.java-----------------------------------
package com.example;
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("Address")
@Expose
private Address address;
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 Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
Now when you want to create the payload, just construct the object of Example class and pass the same to the test method.
Example example = new Example(
"Virender", "Singh", new Address("Line1", "flat no 101", "andheri", "Mumbai")
);
public void RegistrationSuccessful(Example example){
// Method definition
RequestSpecification request = RestAssured.given();
request.body(example);
Response _response = _request.post("//EndPoint");
}
In this way, you can handle much more complex payload. More over all the POJO can be generated from the json.
来源:https://stackoverflow.com/questions/52208765/rest-assured-how-to-pass-object-in-jsonobject-body