json 如何在Rest Assurance中为以下示例设置请求有效负载?

bhmjp9jg  于 2023-01-03  发布在  其他
关注(0)|答案(1)|浏览(123)

我需要设置一个请求有效载荷为(附加图像)在放心,图像标记必须包含一个数组的名称和工作值对,然后是URL字符串数组。我尝试使用POJO,但我不能复制的有效载荷准确。

有效负载示例

{
   "id":1,
   "title":"iPhone 9",
   "description":"An apple mobile which is nothing like apple",
   "price":549,
   "images":[
      {
         "name":"aaa",
         "job":"dev"
      },
      "https://i.dummyjson.com/data/products/1/1.jpg",
      "https://i.dummyjson.com/data/products/1/2.jpg"
   ]
}

我试过的密码。

主类

public class PostDataWithoutSerial {
    
    pojoimage p1 = new pojoimage();
    Images i1=new Images();
    List<Images> img = new ArrayList<Images>();
    ArrayList<String> url1 = new ArrayList<String>();
    
    @Test
    public void postRequestWithoutSerial()
    {

        p1.setTitle("google");
        p1.setDescription("google phone");
        p1.setPrice("800");
        i1.setName("james");
        i1.setJob("watt");
        url1.add("aaa");
        url1.add("bbb");
        i1.setUrl(url1);
        img.add(i1);
        p1.setImages(img);

        given().log().all()
        .contentType("application/json")
        .body(p1)
        .when()
        .post("http://localhost:3000/products")
        .then()
        .statusCode(201);
        //validating single value in response

    }
}

pojoimage.java类(获取器和设置器类)

import java.util.ArrayList;
import java.util.List;
import java.util.List;
public class pojoimage {
    public  String id;
    public String title;
    public String description;
    public String price;
    public List< Images> images;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public String getPrice() {
        return price;
    }
    public void setPrice(String price) {
        this.price = price;
    }
    public List<Images> getImages() {
        return images;
    }
    public void setImages(List<Images> images) {
        this.images = images;
    }
}

图像. java类

import java.util.ArrayList;

public class Images {
    public String name;
    public String job;
    ArrayList<String> url = new ArrayList<String>();
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getJob() {
        return job;
    }
    public void setJob(String job) {
        this.job = job;
    }
    public ArrayList<String> getUrl() {
        return url;
    }
    
    public void setUrl(ArrayList<String> url1) {
        TODO Auto-generated method stub
        this.url = url1;
    }
}
ttisahbt

ttisahbt1#

由于json中的images包含json objectstring,因此正确的数据类型是List<Object>。在此解决方案中,我使用Map<>来简化json对象的设置数据,您可以使用POJO来替换。

import lombok.Data;

@Data
static class PojoImage {
    public  int id;
    public String title;
    public String description;
    public int price;
    public List<Object> images;
}
@Test
public void postRequestWithoutSerial() {
    PojoImage pojoImage = new PojoImage();
    pojoImage.setId(1);
    pojoImage.setTitle("iPhone 9");
    pojoImage.setDescription("An apple mobile which is nothing like apple");
    pojoImage.setPrice(549);

    List<Object> images = new ArrayList<>();
    images.add(Map.of("name", "aaa", "job", "dev"));
    images.add("https://i.dummyjson.com/data/products/1/1.jpg");
    images.add("https://i.dummyjson.com/data/products/1/2.jpg");

    pojoImage.setImages(images);

    given().log().all()
            .contentType("application/json")
            .body(pojoImage)
            .when()
            .post("https://postman-echo.com/post");
}

相关问题