我正在使用Grails 1.3.6。我的服务中有此代码...
if (folder) {
path = "json/${folder}/${page}.json"
} else {
path = "json/${page}.json"
} // if
def file = ApplicationHolder.application.parentContext.getResource(path).getFile();
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) )
}
最佳答案
这可能是因为测试和Web应用程序的上下文路径不同。尝试改用resource tag:
if (folder) {
path = "${resource(dir:'json',file:"${folder}/${page}.json")}"
} else {
path = "${resource(dir:'json',file:"${page}.json")}"
}
https://stackoverflow.com/questions/6583453/