grails – 如何使用MockFor在HttpBuilder上模拟客户端?
发布时间:2020-12-14 16:21:29 所属栏目:大数据 来源:网络整理
导读:我有一些Grails生产代码我想测试,我在这里看到了一些例子,见下文.问题是我在HttpBuilder的客户端部分遇到了MissimgMethodExceptions.这是我要测试的生产代码: class RunkeeperActivitiesRetrieverService { def http = new RESTClient("http://api.runkeepe
我有一些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 docs中的模拟接口: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:没有方法签名:$Proxy15.clearRequestInterceptors()适用于参数类型:()值:[] 我在这里错过了什么? 解决方法
你正在向HttpClient投射impl – 它似乎没有出现在
that interface上定义的问题.也许你的意思是使用
AbstractHttpClient 的某个子类.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |