我有一些我想测试的Grails生产代码,我在这里看到了一些例子,见下文。问题是我在HttpBuilder的客户端部分遇到了MissimgMethodExceptions。这是我想测试的生产代码:
class RunkeeperActivitiesRetrieverService {
def http = new RESTClient("http://api.runkeeper.com/")
def getActivities() {
http.client.clearRequestInterceptors();
http.client.addRequestInterceptor(new HttpRequestInterceptor() {
void process(HttpRequest httpRequest, HttpContext httpContext) {
httpRequest.addHeader('Authorization', 'Bearer xxxxxxxxxx')
}
})
//Do more with the httpBuilder
}
}
我在这里找到了一些关于模拟HTTPBuilder主题的帮助:Groovy HTTPBuilder Mocking the Response以及一些关于groovy文档中的模拟接口:http://groovy.codehaus.org/Groovy+way+to+implement+interfaces
使用该代码我来到了这个测试代码:
def mock = new MockFor(HTTPBuilder)
def impl = [
addRequestInterceptor : {HttpRequestInterceptor interceptor -> println "hi 4"+interceptor},
clearRequestInterceptors : {println "hi 88"}
]
def client = impl as HttpClient
mock.demand.getClient {return client}
mock.use{
service.http = new HTTPBuilder()
println service.getActivities()
}
不幸的是,我对Groovy的了解太有限,无法解决抛出的异常:groovy.lang.MissingMethodException: No signature of method: $Proxy15.clearRequestInterceptors() is applicable for argument types: () values: []
我在这里缺少什么?
答案 0 :(得分:1)
您将impl
投射到HttpClient
- 似乎未在that interface上定义相关方法。也许你的意思是使用AbstractHttpClient
的一些子类。