production environment - Use CommonsMultipartResolver in Spring and config in test -



production environment - Use CommonsMultipartResolver in Spring and config in test -

i have integration test create request (uploading file) on controller. test works without setup commonsmultipartresolver. in moment have setup production environment have add together commonsmultipartresolver. has side effect test don't work. sames xml configuration needed production, , not test. know there possibility define profiles test , production environment. there other possibility not have profile?

the configuration of multipartresolver simple:

<bean id="multipartresolver" class="org.springframework.web.multipart.commons.commonsmultipartresolver" p:maxuploadsize="1000000000"> </bean>

and test simple:

mockmultipartfile afileobject = new mockmultipartfile("file", "filename.txt", "text/plain", "a file message".getbytes()); hashmap<string, string> contenttypeparams = new hashmap<string, string>(); contenttypeparams.put("boundary", "xyz"); mediatype mediatype = new mediatype("multipart", "form-data", contenttypeparams); mockhttpservletrequestbuilder action = fileupload(path).file(afileobject)); mockmvc=mockmvcbuilders.webappcontextsetup(webapplicationcontext) .addfilter(new delegatingfilterproxy("springsecurityfilterchain", webapplicationcontext), "/*") .build() resultactions resultpost =mockmvc.perform(action.contenttype(mediatype)); assertthat(.....

(i have simplified little bit test code (this not issue here. works.) )

has thought how can figure out configuration of multipartresolver when test running, , enabling when set in production without have remember every time comment config?

mockmvc not run servlet container. uses mockhttpservletrequest/response means set request manually. includes file uploads. building request fileupload(..).file(..) you're manually setting mockmultiparthttpservletrequest (same thing multipartresolver @ runtime in actual servlet container). when dispatcherservlet processes request, realizes request has been parsed multipart request, , happy move on without having phone call multipartresolver.

bottom line, if want test controller multipart request, should work fine (see illustration test: https://github.com/spring-projects/spring-framework/blob/master/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/fileuploadcontrollertests.java). if want test actual upload multipartresolver getting involved, you'll need write integration test in-memory server. chances former need cases , 1 test real server ensure resolver in configuration should okay.

production-environment spring-test spring-data-rest xml-configuration test-environments

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -