9

我正在尝试为使用 grailsApplication.config 进行一些设置的服务编写单元测试。似乎在我的单元测试中,服务实例无法访问其设置的配置文件(空指针),而当我运行“run-app”时它可以访问该设置。如何在单元测试中配置服务以访问 grailsApplication 服务。

class MapCloudMediaServerControllerTests {

    def grailsApplication

    @Before
    public void setUp(){
        grailsApplication.config= 
        '''
   video{
   location="C:\\tmp\\"  // or shared filesystem drive for a cluster

    yamdi{
        path="C:\\FFmpeg\\ffmpeg-20121125-git-26c531c-win64-static\\bin\\yamdi"  
         }

    ffmpeg  {
        fileExtension = "flv"  // use flv or mp4
        conversionArgs = "-b 600k -r 24 -ar 22050 -ab 96k"
        path="C:\\FFmpeg\\ffmpeg-20121125-git-26c531c-win64-static\\bin\\ffmpeg"
        makethumb = "-an -ss 00:00:03 -an -r 2 -vframes 1 -y -f mjpeg"
    }

    ffprobe {
        path="C:\\FFmpeg\\ffmpeg-20121125-git-26c531c-win64-static\\bin\\ffprobe" 
        params=""
    }

    flowplayer {
        version = "3.1.2" 
    }

    swfobject {
        version = "" 

    qtfaststart {
        path= "C:\\FFmpeg\\ffmpeg-20121125-git-26c531c-win64-static\\bin\\qtfaststart" 
    } 
}   '''
    }

@Test
    void testMpegtoFlvConvertor() {

        log.info "In test Mpg to Flv Convertor function!"

        def controller=new MapCloudMediaServerController()
        assert controller!=null

        controller.videoService=new VideoService()  
        assert controller.videoService!=null

        log.info "Is the video service null? ${controller.videoService==null}"

        controller.videoService.grailsApplication=grailsApplication

        log.info "Is grailsApplication null? ${controller.videoService.grailsApplication==null}"

        //Very important part for simulating the HTTP request
        controller.metaClass.request = new MockMultipartHttpServletRequest()
        controller.request.contentType="video/mpg"
        controller.request.content= new File("..\\MapCloudMediaServer\\web-app\\videoclips\\sample3.mpg").getBytes()

        controller.mpegtoFlvConvertor()

        byte[] videoOut=IOUtils.toByteArray(controller.response.getOutputStream())
        def outputFile=new File("..\\MapCloudMediaServer\\web-app\\videoclips\\testsample3.flv")
        outputFile.append(videoOut) 
    }
}
4

3 回答 3

19

如果您使用@TestForGrails (2.0) 已经为您模拟 grailsApplication,只需设置您的配置,但不要声明 grailsApplication。这样做会覆盖模拟实例。

@TestFor(MyService)
class MyServiceTests {

  @Before
  public void setUp() {
    grailsApplication.config.something = "something"
  }

  @Test
  void testSomething() {
    MyService service = new MyService()
    service.grailsApplication = grailsApplication
    service.doSomething()
  }


}

编辑

您声明了一个String,要添加到配置中,您必须解析它。请参阅此处的示例。

基本上,您使用ConfigSlurper().parse()来获取ConfigObject,并使用grails.config.merge()将内容添加到配置中。

于 2012-12-12T18:21:14.653 回答
4

使用DefaultGrailsApplication构建 grailsApp会起作用。

class ZazzercodeUnitTestCase extends GrailsUnitTestCase {
    def grailsApplication = new org.codehaus.groovy.grails.commons.DefaultGrailsApplication()
    def chortIndex= grailsApplication.config.zazzercode.chortIndex
}
于 2013-12-06T07:26:32.433 回答
1

转到http://ilikeorangutans.github.io/2014/02/06/grails-2-testing-guide

Grails 的配置通常使用 config 属性通过注入的 GrailsApplication 实例进行访问。Grails 将 GrailsApplication 注入到单元测试中,因此您可以直接访问它:

@TestMixin(GrailsUnitTestMixin)
class MySpec extends Specification {        
    private static final String VALUE = "Hello"
    void "test something with config"() {
        setup:
        // You have access to grailsApplication.config so you can 
        //modify these values as much as you need, so you can do
        grailsApplication.config.myConfigValue = VALUE

        assert:             
        grailsApplication.config.myConfigValue == VALUE
    }

}
于 2017-09-26T15:49:20.827 回答