开发者

Trouble in integration test finding resource

I'm using Grails 1.3.6. I have this code in my service ...

            if (fold开发者_Python百科er) {
                    path = "json/${folder}/${page}.json"
            } else {
                    path = "json/${page}.json"
            } // if
            def file = ApplicationHolder.application.parentContext.getResource(path).getFile();

Normally the file is correctly retrieved from the web-app/json directory, but when I run an integration test (through grails test-app), the above code fails when trying to retrieve the resource ("java.io.FileNotFoundException: class path resource [json/index.json] cannot be resolved to URL because it does not exist"). Below is my integration test. How do I tell it where to find the resource?

Thanks, - Dave

class HomeControllerTests extends GroovyTestCase {
...

void testGetAllJSON() {
            def controller = new HomeController()

            // Call action without any parameters
            controller.index()

            def responseStr = controller.response.contentAsString

            assertTrue( isValidJSON(responseStr) )
    } 


That may be because the context path is different for the tests versus web-app. Try instead using the resource tag:

if (folder) {
    path = "${resource(dir:'json',file:"${folder}/${page}.json")}"
} else {
    path = "${resource(dir:'json',file:"${page}.json")}"
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜