grails – 如何使用MockFor在HttpBuilder上模拟客户端?

我有一些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的某个子类.

相关文章

背景:    8月29日,凌晨4点左右,某服务告警,其中一个...
https://support.smartbear.comeadyapi/docs/soapui/steps/g...
有几个选项可用于执行自定义JMeter脚本并扩展基线JMeter功能...
Scala和Java为静态语言,Groovy为动态语言Scala:函数式编程,...
出处:https://www.jianshu.com/p/ce6f8a1f66f4一、一些内部...
在运行groovy的junit方法时,报了这个错误:java.lang.Excep...