Dave
Dave

Reputation: 8897

Trouble in integration test finding resource

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

            if (folder) {
                    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) )
    } 

Upvotes: 1

Views: 311

Answers (1)

Igor
Igor

Reputation: 33993

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

Upvotes: 1

Related Questions