java - How to PUT multipart/form-data using Spring MockMvc? -


i have controller's method put method, receives multipart/form-data:

   @requestmapping(value = "/putin", method = requestmethod.put)    public foo updatefoo(httpservletrequest request,                            @requestbody foo foo,                            @requestparam("foo_icon") multipartfile file) {     ...    } 

and want test using mockmvc. unfortunately mockmvcrequestbuilders.fileupload creates instance of mockmultiparthttpservletrequestbuilder has post method:

super(httpmethod.post, urltemplate, urlvariables) 

edit: surely can i can not create own implementation of mockhttpservletrequestbuilder, say

public mockputmultiparthttpservletrequestbuilder(string urltemplate, object... urlvariables) {     super(httpmethod.put, urltemplate, urlvariables);     super.contenttype(mediatype.multipart_form_data); } 

because mockhttpservletrequestbuilder has package-local constructor.

but i'm wondering more convenient way this, may missed existent class or method doing it?

yes, there way, , it's simple too!

i ran same problem myself. though discouraged sam brannen's answer, appears spring mvc nowadays support put file uploading such request using postman (i'm using spring boot 1.4.2). so, kept digging , found problem fact mockmultiparthttpservletrequestbuilder returned mockmvcrequestbuilders.fileupload() has method hardcoded "post". discovered with() method...

and allowed me come neat little trick force mockmultiparthttpservletrequestbuilder use "put" method anyway:

    mockmultipartfile file = new mockmultipartfile("data", "dummy.csv",             "text/plain", "some dataset...".getbytes());      mockmultiparthttpservletrequestbuilder builder =             mockmvcrequestbuilders.fileupload("/test1/datasets/set1");     builder.with(new requestpostprocessor() {         @override         public mockhttpservletrequest postprocessrequest(mockhttpservletrequest request) {             request.setmethod("put");             return request;         }     });     mvc.perform(builder             .file(file))             .andexpect(status().ok()); 

works charm!


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -