当使用pathParameters来记录URI路径参数时,如下所示
@Test public void documentGetRouteById() throws Exception { this.mockMvc.perform(get("/route/{id}","FooBar")).andExpect(status().isOk()) .andDo(document("api-getRouteById",pathParameters(parameterWithName("id").description("die Routen ID")))); }
我得到以下的兴趣
java.lang.IllegalArgumentException: urlTemplate not found. Did you use RestDocumentationRequestBuilders to build the request? at org.springframework.util.Assert.notNull(Assert.java:112) at org.springframework.restdocs.request.PathParameteRSSnippet.extractUrlTemplate(PathParameteRSSnippet.java:95) at org.springframework.restdocs.request.PathParameteRSSnippet.extractActualParameters(PathParameteRSSnippet.java:82) at org.springframework.restdocs.request.AbstractParameteRSSnippet.verifyParameterDescriptors(AbstractParameteRSSnippet.java:77) at org.springframework.restdocs.request.AbstractParameteRSSnippet.createModel(AbstractParameteRSSnippet.java:65) at org.springframework.restdocs.request.PathParameteRSSnippet.createModel(PathParameteRSSnippet.java:67) at org.springframework.restdocs.snippet.TemplatedSnippet.document(TemplatedSnippet.java:64) at org.springframework.restdocs.mockmvc.RestDocumentationResultHandler.handle(RestDocumentationResultHandler.java:101) at org.springframework.test.web.servlet.MockMvc$1.andDo(MockMvc.java:158)
我很确定我做了测试设置,如解释here.
我可能做错了什么?
(Spring REST文档版本为1.0.0.BUILD-SNAPSHOT)
解决方法
异常信息正在试图指向正确的方向:
urlTemplate not found. Did you use RestDocumentationRequestBuilders to build the request?
您需要使用RestDocumentationRequestBuilders,以便Spring REST文档可以捕获URL并从中提取参数.这是mentioned in the documentation,它说:
To make the path parameters available for documentation,the request must be built using one of the methods on RestDocumentationRequestBuilders rather than MockMvcRequestBuilders.
将静态导入的MockMvcRequestBuilders.get替换为一个用于RestDocumentationRequestBuilders.get应该可以解决问题.