我真的很喜欢使用失败的测试来确保文档是最新的概念。但我不知道如何使它适用于嵌套的 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 和 suspend.type 没有定义,使用这些响应字段定义的测试也会通过。有没有办法让它工作?多次测试?