How to test file upload in Grails

I have a controller that takes a file as part of a parameter. I am wondering how can I check this?

my controller action:

def save () { def colorInstance = new Color(params.color) CommonsMultipartFile file = request.getFile('color.filename') fileUploadService.upload(colorInstance, file, "foldername") if (requestInstance.save(flush: true)) { withFormat { html {redirect(action: "list") } js {render "test"} } } } 

I started with something like this: ...

 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 } } 

Question

  • I cannot figure out how to test CommonsMultipartFile to download a file.
  • In addition, this test case will be located in the unit test folder. How can i execute it?
+4
source share
3 answers

Since the request will be multipart during file download, the actual request servlet will be MultipartHttpServletRequest . For the unit test case, mock the same and use it as request in the controller . With a successful mockery, 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 } } 
+5
source

I am using Grails 2.4 and you can just use the GrailsMockMultipartFile and request.addFile method in unit test.

This code runs on Grails 2.4.4 with the Spock testing environment:

Controller side:

 class FileUploadController { def upload() { def multipartFile = request.getFile('requestParamName') if (multipartFile.empty) { flash.message = 'Error: file cannot be empty' render(view: 'upload') return } // do something now with your file } } 

Unit test side:

 import grails.test.mixin.TestFor import org.codehaus.groovy.grails.plugins.testing.GrailsMockMultipartFile import spock.lang.Specification @TestFor(FileUploadController) class FileUploadControllerSpec extends Specification { void "upload should add flash error message if empty file in request"() { given: def multipartFile = new GrailsMockMultipartFile('requestParamName', 'someExcelFile.xls', 'application/vnd.ms-excel', new byte[0]) request.addFile(multipartFile) when: controller.upload() then: assertEquals('Error: file cannot be empty', flash.message) } } 
+5
source

The accepted answer did not help me. The following code did:

 import org.junit.Before import grails.test.mixin.Mock import grails.test.mixin.TestFor import org.springframework.mock.web.MockMultipartHttpServletRequest import org.springframework.mock.web.MockMultipartFile @TestFor(ColorController) class ColorControllerTests { def mockRequest @Before void setUp() { mockRequest = new MockMultipartHttpServletRequest() controller.metaClass.request = mockRequest // your service here controller.fileUploadService = new FileUploadService } void testFileUpload(){ //Add a mock multipart file to request mockRequest.addFile(new MockMultipartFile('myFile', 'IronMan3.jpg', 'image/jpeg', "1234567" as byte[])) //call the controller action //assert response } } 
+1
source

Source: https://habr.com/ru/post/1480340/


All Articles