我将 spring 单元测试与 spring-restdocs 一起使用。
这是我的 mockmvc 代码:
mockMvc.perform(fileUpload("/api/enterprise/uploadImage")
.file(imageFile)
.with(csrf().asHeader())
.params(params)
).andExpect(status().isOk());
但是当使用 spring-restdocs 时,我不知道如何编写文件片段。
这是我的片段创建代码:
document.snippets(
requestParameters(
parameterWithName("file").description("upload file"),
parameterWithName("imageType").description("image type")
)
);
这样我得到一个错误:
org.springframework.restdocs.snippet.SnippetException: Request parameters with the following names were not found in the request: [file]
at org.springframework.restdocs.request.RequestParametersSnippet.verificationFailed(RequestParametersSnippet.java:79)
at org.springframework.restdocs.request.AbstractParametersSnippet.verifyParameterDescriptors(AbstractParametersSnippet.java:93)
at org.springframework.restdocs.request.AbstractParametersSnippet.createModel(AbstractParametersSnippet.java:70)
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.applyDefaultResultActions(MockMvc.java:195)
at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:163)
at com.athena.edge.enterprise.controller.UploadImageTest.uploadImage(UploadImageTest.java:108)