下面的代码执行时
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");
}
回报
{
"FirstName": "Virender",
"LastName": "Singh"
}
有人可以指导以下JSON的保证代码吗?
{
"FirstName": "Virender",
"LastName": "Singh",
"Address": {
"Line1": "flat no 101",
"area": "andheri",
"City": "Mumbai"
}
}
您可以使用JSONObject,HashMap或POJO
使用JSONObject的示例代码,我还没有测试下面的代码,所以让我知道它是否不起作用
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");
也是使用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");
处理嵌套json的实用方法是通过POJO序列化json。给定json的POJO将是:[这里我使用的是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;
}
}
现在,当您想要创建有效负载时,只需构造Example类的对象并将其传递给测试方法。
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");
}
通过这种方式,您可以处理更复杂的有效负载。更多的POJO可以从json生成。