【发布时间】:2016-07-07 08:30:05
【问题描述】:
我真的很喜欢使用失败的测试来确保文档是最新的概念。但我不知道如何使它适用于嵌套的 json。 Spring REST Docs 处理分层有效负载的方式似乎违背了目的:
记录字段时,如果在有效负载中发现未记录的字段,则测试将失败。同样,如果在有效负载中找不到记录的字段并且该字段未标记为可选,则测试也将失败。 对于具有层次结构的有效负载,记录一个字段足以使其所有后代也被视为已记录。
您将如何为嵌套 json 编写测试,以便对有效负载的更改导致测试失败?
例子:
{
car: {
motor : {
brand: "Porsche",
power: "165 kW"
},
suspension: {
type: "automatic"
}
}
测试:
.andDo(document("mytest", responseFields(
fieldWithPath("car").description("the car").type(JsonFieldType.OBJECT),
fieldWithPath("car.motor").description("the motor").type(JsonFieldType.OBJECT),
fieldWithPath("car.motor.brand").description("the motor brand").type(JsonFieldType.STRING),
fieldWithPath("car.suspension").description("the suspension"))))
即使未定义 car.motor.power 和suspension.type,使用这些响应字段定义的测试也会通过。有没有办法让它工作?多个测试?
【问题讨论】:
标签: json spring spring-restdocs