Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grailsApplication access in Grails unit Test

Tags:

I am trying to write unit tests for a service which use grailsApplication.config to do some settings. It seems that in my unit tests that service instance could not access the config file (null pointer) for its setting while it could access that setting when I run "run-app". How could I configure the service to access grailsApplication service in my unit tests.

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