使用 MockFor 如何为重载方法编写单元测试。
例如
public HttpResponse execute(HttpUriRequest request, HttpContext context)
和
public HttpResponse execute(HttpHost target, HttpRequest request)
我已经写了:
def responseMocker = new MockFor(HttpResponse.class)
HttpResponse response = responseMocker.proxyInstance()
def requestMocker = new MockFor(HttpUriRequest.class)
def request = requestMocker.proxyInstance()
def contextMocker = new MockFor(HttpContext.class)
def context = contextMocker.proxyInstance();
def clientMocker = new MockFor(HttpClient.class)
clientMocker.demand.execute(request, context) { response }
HttpClientSample client = new HttpClientSample(clientMocker.proxyInstance())
assertEquals response, client.execute(request,context)
上面的这个测试失败了:
groovy.lang.MissingMethodException:没有方法签名:....