Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Lombok反序列化POJO以发送大型JSON负载_Java_Json_Builder_Lombok_Rest Assured - Fatal编程技术网

Java 使用Lombok反序列化POJO以发送大型JSON负载

Java 使用Lombok反序列化POJO以发送大型JSON负载,java,json,builder,lombok,rest-assured,Java,Json,Builder,Lombok,Rest Assured,我是一名QA,使用放心DSL编写一些测试 这是我第一次尝试使用Lombok反序列化POJO,以便在JSON负载中使用 这种构建数据对象Customer的方法似乎非常麻烦。由于400的测试失败,我假设我没有正确地序列化它,并且不清楚如何将负载视为JSON 我没有使用显式映射,所以假设Rest-Assured默认使用GSON 鉴于我的POJO: import lombok.Data; @Data public class Customer { private String employe

我是一名QA,使用放心DSL编写一些测试

这是我第一次尝试使用Lombok反序列化POJO,以便在JSON负载中使用

这种构建数据对象Customer的方法似乎非常麻烦。由于400的测试失败,我假设我没有正确地序列化它,并且不清楚如何将负载视为JSON

我没有使用显式映射,所以假设Rest-Assured默认使用GSON

鉴于我的POJO:

import lombok.Data;

@Data
public class Customer {

    private String employeeCode;
    private String customer;
    private String firstName;
    private String lastName;
    private String title;
    private String dob;
    private String employeeId;

}
…以及我需要发送的示例有效负载:

{
    "employeeCode": "18ae56",
    "customer": {
        "firstName": "John",
        "lastName": "Smith",
        "title": "Mr",
        "dob": "1982-01-08", 
        "employeeId": "2898373"
    }
}
我的示例测试是:

 @BeforeClass
 public static void createRequestSpecification(){

    requestSpec = new RequestSpecBuilder()
            .setBaseUri("https://employee-applications.company.com")
            .setContentType(ContentType.JSON)
            .build();
}

   @Test
    public void createApplicationForNewCustomer(){

    Customer customer = Customer.builder().build();
    customer.setEmployeeCode("18ae56");
    customer.setFirstName("John");
    customer.setLastName("Smith");
    customer.setTitle("Mr");
    customer.setDob("1982-01-08");
    customer.setEmployeeId("2898373");

    given().
            spec(requestSpec).
    and().
            body(customer).
    when().
            post("/api/v6/applications").
    then().
            assertThat().statusCode(201);

}

您的POJO不正确,显然序列化的JSON不是预期的格式

你应该上两节课

下面是生成给定JSON结构的POJO的外观

    @Data
    public static class Customer {

        @JsonProperty("firstName")
        private String firstName;
        @JsonProperty("lastName")
        private String lastName;
        @JsonProperty("title")
        private String title;
        @JsonProperty("dob")
        private String dob;
        @JsonProperty("employeeId")
        private String employeeId;

    }

    @Data
    public static class Example {

        @JsonProperty("employeeCode")
        public String employeeCode;
        @JsonProperty("customer")
        public Customer customer;

    }
你的测试方法呢

Example e = new Example();
e.setEmployeeCode("18ae56");

Customer c = new Customer();
c.setFirstName("John");
c.setLastName("Smith");
c.setTitle("Mr");
c.setDob("1982-01-08");
c.setEmployeeId("2898373");

e.setCustomer(c);


given().spec(requestSpec).and().body(e).when().post("/api/v6/applications").then().assertThat()
最简单的测试方法:

String abc = new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(e);
System.out.println(abc);


我看这里没有问题。你说“我不清楚我是否正确地序列化了它”,我首先假设如果你的测试通过了,你就是。否则,如果输入不相关,你在测试什么?@Michael道歉,不清楚;现在更新。我正在尝试调试,只是想更快地到达那里!你的课看起来绝对不对。没有什么可以说明嵌套结构。您需要两个类:一个名为Message的新类,它包含两个字段:
String employeeId
,和
Customer
。然后从customer类中删除employeeId简单的测试方法:按照
System.out.println(new Gson().toJson(customerMessage))
@Michael的思路创建一个新的测试方法-我已经在我的答案中添加了这一点,我通常使用
ObjectMapper()
来测试,多亏了你,我现在知道了另一种方法:)太好了!非常感谢。请原谅我的无知,我无法在测试类中使类静态化,因此实例化了示例和客户
System.out.println(new Gson().toJson(e));