Reputation: 41
I use spring unit test with spring-restdocs.
this is my mockmvc code:
mockMvc.perform(fileUpload("/api/enterprise/uploadImage")
.file(imageFile)
.with(csrf().asHeader())
.params(params)
).andExpect(status().isOk());
but when use spring-restdocs I don't how to write the snippet of file filed.
this is my snippets create code:
document.snippets(
requestParameters(
parameterWithName("file").description("upload file"),
parameterWithName("imageType").description("image type")
)
);
in this way I get an error:
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)
Upvotes: 4
Views: 3692
Reputation: 4262
Since the release of version 1.1.0.RELEASE
of spring-restdocs
you can use RequestPartsSnippet
.
You can now write spring-restdocs snippets with MockMultipartFile as the following:
mockMvc.perform(multipart("/upload").file("file", "example".getBytes()))
.andExpect(status().isOk())
.andDo(document("upload", RequestPartsSnippet.requestParts(
RequestPartDescriptor.partWithName("file").description("The file to upload"))
));
This example is taken from the official documentation here.
Upvotes: 2
Reputation: 116061
You're sending a multi-part request so the file that's being uploaded isn't a request parameter. Instead, it's one of the parts in the request and your test is failing because you're trying to document a request parameter that doesn't exist.
Spring REST Docs doesn't have support for documenting parts in a multipart request at the moment. There is an open issue for it. I haven't implemented anything yet as request parts can be quite complex. For example, in some cases the part name and a description may be sufficient but in others it may be useful to document the part's headers, the structure of its content, etc.
Please comment on the issue linked to above, particularly if support for the simplest case would be useful.
Upvotes: 2