问题描述
我正在尝试为使用grailsApplication.config做一些设置的服务编写单元测试。看起来在我的单元测试中,服务实例无法访问配置文件(空指针)的设置,而当我运行run-app时它可以访问该设置。
类MapCloudMediaServerControllerTests {
如何配置服务以在我的单元测试中访问grailsApplication服务。 def grailsApplication
@Before
public void setUp(){
grailsApplication.config =
'''
video {
location = C:\\\\\\\\\\\//或共享文件系统驱动器为一个群集
yamdi {
path =C:\\FFmpeg\\ffmpeg -20121125-git-26c531c-win64-static\\bin\\yamdi
}
ffmpeg {
fileExtension =flv//使用flv或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:\\F Fmpeg \\ffmpeg-20121125-git-26c531c-win64-static \\\\\\\\\\\\\\\\\\\\\\\'
version =3.1.2
}
swfobject {
version =
qtfaststart {
path = C:\\FFmpeg \\ffmpeg-20121125-git-26c531c-win64-static \\\\\\\\\\\\''b $ b $'$ b $'
}
@Test
void testMpegtoFlvConvertor(){
log.info在测试Mpg到Flv转换器函数!
def controller = new MapCloudMediaServerController()
assert controller!= null
controller.videoService = new VideoService()
assert controller.videoService!= null
log.info视频服务是否为空?$ {controller.videoService == null}
controller.videoService.grailsApplication = grailsApplication
log.info是grailsApplication null?$ {controller.videoService.grailsApplication == null}
//用于模拟HTTP请求的非常重要的部分
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.get OutputStream())
def outputFile = new File(.. \\MapCloudMediaServer\\web-app\\videoclips\\testsample3.flv)
outputFile.append (videoOut)
}
}
@TestFor Grails(2.0)已经为您模拟grailsApplication,只需设置您的配置,但不要声明grailsApplication。这样做会覆盖模拟的实例。
@TestFor(MyService)$ b $ 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()
将内容添加到配置中。
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)
}
}
If you use @TestFor
Grails (2.0) already mock grailsApplication for you, just set your configs, but do not declare the grailsApplication. Doing that overrides the mocked instance.
@TestFor(MyService)
class MyServiceTests {
@Before
public void setUp() {
grailsApplication.config.something = "something"
}
@Test
void testSomething() {
MyService service = new MyService()
service.grailsApplication = grailsApplication
service.doSomething()
}
}
EDIT:
You declared a String
, to add to config you must parse this. See here an example.
Basically you use the ConfigSlurper().parse()
to get a ConfigObject
, and use grails.config.merge()
to add the contents to the config.
这篇关于Grails单元测试中的grailsApplication访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!