Since the request will be multipart
during file upload, the actual request servlet would be MultipartHttpServletRequest
. For unit test case, mock
the same and use it as the request
in controller
. On successful mocking you should be able to addFile
in tests and getFile
in action
.
import org.junit.Before
import grails.test.mixin.Mock
import grails.test.mixin.TestFor
@TestFor(ColorController)
@Mock(Color)
class ColorControllerTests {
@Before
void setUp() {
controller.fileUploadService = new FileUploadService
//Mock MultipartHttpServletRequest somethign like below
controller.metaClass.request = mockFor(MultipartHttpServletRequest).createMock()
}
void testFileUpload(){
//Add a mock multipart file to request
controller.request.addFile(new MockMultipartFile('myFile', 'IronMan3.jpg', 'image/jpeg', "1234567" as byte[]))
//call the controller action
//assert response
}
}