编写单元测试用例时,我需要做一些元编程来测试下面的方法。
void "test method:resolver"(){
setup:"mocked resolver"
ContextHolder.getMetaClass().static.getBean = {
Resolver resolver = Mock(Resolver)
resolver(_) >> {HttpServletRequest request1->
return 1;
}
}
and:"mocked getAppName"
CoreUtil.metaClass.static.getAppName = {
return "$apiName"
}
when:
UserGroupRole userGroupRole = service.resolve(username)
then:
userGroupRole != null
where:
apiName || username
"core-blog" || "test11"
}
以下是我运行测试用例时遇到的情况:
grails test-app:unit
它无法说Class.metaclass.static say can not get static on null
请帮忙!