我的应用程序正在使用
spring-data-rest
和
spring-restdocs
.
我的设置非常标准;几乎完全从文档中复制,但我已经包括了下面的示例,以防遗漏一些东西。
当我的mvc测试运行时,它失败了:
org.springframework.restdocs.snippet.SnippetException: The following parts of the payload were not documented:
{
"_links" : {
"self" : {
"href" : "https://my-api/item/10"
},
"item" : {
"href" : "https://my-api/item/10"
}
}
}
这是我的测试代码:
@Rule
public JUnitRestDocumentation restDocs = new JUnitRestDocumentation("target/generated-snippets");
mockMvc = webAppContextSetup(wac)
.apply(documentationConfiguration(restDocs)
.uris()
.withHost("my-api")
.withPort(443)
.withScheme("https"))
.build();
mockMvc.perform(get("/items/{id}", "10"))
.andDo(documentation)
下面是堆栈:
at org.springframework.restdocs.payload.AbstractFieldsSnippet.validateFieldDocumentation(AbstractFieldsSnippet.java:176)
at org.springframework.restdocs.payload.AbstractFieldsSnippet.createModel(AbstractFieldsSnippet.java:100)
at org.springframework.restdocs.snippet.TemplatedSnippet.document(TemplatedSnippet.java:64)
at org.springframework.restdocs.generate.RestDocumentationGenerator.handle(RestDocumentationGenerator.java:196)
at org.springframework.restdocs.mockmvc.RestDocumentationResultHandler.handle(RestDocumentationResultHandler.java:55)
at org.springframework.test.web.servlet.MockMvc$1.andDo(MockMvc.java:177)
at com.example.my.api.domain.MyRepositoryRestTest.findOne(MyRepositoryRestTest.java:36)
我怎么得到
SpringRestDocs
和
spring数据休息
玩得好吗?
我的
documentation
实例定义如下:
ResultHandler documentation = document("items/findOne",
preprocessRequest(prettyPrint(), maskLinks()),
preprocessResponse(prettyPrint()),
responseFields(
fieldWithPath("name").description("Item name.")
));
the restdocs docs for ignoring links
我可以补充说
links(linkWithRel("self").ignored(),
linkWithRel("_self").ignored().optional()) // docs suggest this. /shrug
但这仍然留给我:
SnippetException: Links with the following relations were not documented: [item]
似乎
_links
我们总是会将自我引用返回到同一实体,对吗?
如何在不忽略每个测试的实体特定链接的情况下干净地处理此问题,如:
links(linkWithRel("item").ignored())
做
添加上述行(以便所有字段
self
_self
curies
和
item
都是
ignored()
和/或
optional()
),测试结果返回到问题顶部的原始错误。