I have a service method which accepts CommonsMultipartFile and uploads it to server
class ExampleService() {
def saveFile(CommonsMultipartFile file) {
// some validation code for file
}
}
tried using both MockMultipartHttpServletRequest and GrailsMockMultipartFile.
In case of MockMultipartHttpServletRequest getting error as:
| org.codehaus.groovy.runtime.typehandling.GroovyCastException:
Cannot cast object 'org.springframework.mock.web.MockMultipartFile@2f39360'
with class 'org.springframework.mock.web.MockMultipartFile' to
class 'org.springframework.web.multipart.commons.CommonsMultipartFile'
Same goes with GrailsMockMultipartFile getting error as:
| org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot
cast object 'org.codehaus.groovy.grails.plugins.testing.GrailsMockMultipartFile@1022f0bd'
with class 'org.codehaus.groovy.grails.plugins.testing.GrailsMockMultipartFile' to
class 'org.springframework.web.multipart.commons.CommonsMultipartFile'
referred this stack overflow question.
How should I mock CommonsMultipartFile and pass it as argument inside my test case ?
Solution
I found solution please check my answer
saveFile()method to use the interfaceMultipartFilerather than the specific implementation? If so, it should be easier to mock. - cjstehnoListandMapinstead ofArrayListandHashMap, andMultipartFilein this case. Only use implemtation class names when instantiating and when accessing methods that aren't in an interface - Burt BeckwithsaveFile()method do some additional code other than saving a file. I don't want to change my existing method implementation. - Laxmi Salunkhe