4

テストしたい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の応答のモックと、ここの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: []

ここで何が欠けていますか?

4

1 に答える 1

1

キャストimpl先-問題のメソッドがそのインターフェースHttpClientで定義されているようには見えません。おそらく、のサブクラスを使用することを意味します。AbstractHttpClient

于 2012-12-09T23:32:17.203 に答える