使用 Lombok 反序列化 POJO 以发送大 JSON 负载
De-serialize a POJO using Lombok to send large JSON payload
我是一名 QA,使用 Rest Assured 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 不是预期格式
你应该有两个 类
下面是您的 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));
我是一名 QA,使用 Rest Assured 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 不是预期格式
你应该有两个 类
下面是您的 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));