如何在Grails中测试文件上传

时间:2013-05-12 15:18:25

标签: unit-testing testing grails junit

我有一个控制器,它接收一个文件作为参数的一部分。我想知道我该怎么测试呢?

我的控制器动作:

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

我从这样的事情开始:...

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

问题

  • 我无法弄清楚如何测试CommonsMultipartFile文件上传。
  • 此外,此测试用例将位于unit测试文件夹中。我该怎么执行呢?

3 个答案:

答案 0 :(得分:5)

由于在文件上载期间请求将为multipart,因此实际请求servlet将为MultipartHttpServletRequest。对于单元测试用例,mock相同,并将其用作request中的controller。成功模拟后,您应该能够addFile进行测试,getFile进入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
    }
}

答案 1 :(得分:5)

我使用的是Grails 2.4,您只需在单元测试中使用GrailsMockMultipartFilerequest.addFile方法。

此代码适用于Grails 2.4.4,Spock测试框架:

控制器端:

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

单元测试方:

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

}

答案 2 :(得分:1)

接受的答案对我不起作用。以下代码执行了:

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