【发布时间】: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 employeeId和Customer customer。然后从客户类中删除employeeId -
简单的测试方法:按照
System.out.println(new Gson().toJson(customerMessage))的思路创建一个新的测试方法 -
@Michael - 我已将这一点添加到我的答案中,我通常使用
ObjectMapper()进行测试,多亏了你,我现在知道了另一种方式:)
标签: java json builder lombok rest-assured