【问题标题】:De-serialize a POJO using Lombok to send large JSON payload使用 Lombok 反序列化 POJO 以发送大型 JSON 有效负载
【发布时间】:2020-07-03 11:43:06
【问题描述】:

我是一名 QA,正在使用 Rest Assured DSL 编写一些测试。

这是我第一次尝试使用 Lombok 反序列化 POJO 以用于 JSON Payload。

这种构建我的数据对象 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);

}

【问题讨论】:

  • 我在这里没有看到任何问题。你说“我不清楚我是否正确序列化它”,那么我首先假设如果你的测试通过了,那么你就是。否则,如果输入不相关,你在测试什么?
  • @Michael 道歉,不清楚;现在更新了。我正在尝试调试,只是希望更快地到达那里!
  • 你的课肯定看起来不对。没有任何迹象表明嵌套结构。您需要 2 个类:一个名为 Message 的新类,它包含 2 个字段:String employeeIdCustomer customer。然后从客户类中删除employeeId
  • 简单的测试方法:按照System.out.println(new Gson().toJson(customerMessage))的思路创建一个新的测试方法
  • @Michael - 我已将这一点添加到我的答案中,我通常使用ObjectMapper() 进行测试,多亏了你,我现在知道了另一种方式:)

标签: java json builder lombok rest-assured


【解决方案1】:

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

你应该有两个类

下面是您的 POJO 应该如何生成给定的 JSON 结构

    @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);

System.out.println(new Gson().toJson(e));

【讨论】:

  • 太棒了!谢谢你。原谅我的无知,我无法在测试类中将示例和客户实例化为静态类
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-10
  • 1970-01-01
  • 2021-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多