我有一些我想测试的 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: []
我在这里想念什么?