如何构建 com.netflix.client.http.httpresponse 对象
how to build com.netflix.client.http.httpresponse object
我正在写unit test
个案例,使用mocking
个概念,我是mocking RestClient class
原码:
final HttpResponse response = restClient.executeWithLoadBalancer(request);
在Spock
中测试class模拟代码:
restClient.executeWithLoadBalancer(_ as HttpRequest) >> httpResponse
这里我必须构建并设置 httpResponse
。如何构建 HttpResponse
作为HttpResponse is an interface and you can't really build or create instance of it.what you can able to do is probably create instance of class that implements this interface. for eg: HttpClientResponse
您可以构建和设置 httpResponse
任何你喜欢的如下,
ClientResponse clientResponse = Mock(ClientResponse)
MultivaluedMapImpl sampleHeaders = new MultivaluedMapImpl()
sampleHeaders.add("name", "test")
clientResponse.getHeaders() >> sampleHeaders
HttpResponse httpResponse = new HttpClientResponse(clientResponse,null,null)
restClient.executeWithLoadBalancer(_ as HttpRequest) >> httpResponse
旁注,要模拟 ClientResponse,您需要在类路径中添加 objenesis。
我正在写unit test
个案例,使用mocking
个概念,我是mocking RestClient class
原码:
final HttpResponse response = restClient.executeWithLoadBalancer(request);
在Spock
中测试class模拟代码:
restClient.executeWithLoadBalancer(_ as HttpRequest) >> httpResponse
这里我必须构建并设置 httpResponse
。如何构建 HttpResponse
作为HttpResponse is an interface and you can't really build or create instance of it.what you can able to do is probably create instance of class that implements this interface. for eg: HttpClientResponse
您可以构建和设置 httpResponse
任何你喜欢的如下,
ClientResponse clientResponse = Mock(ClientResponse)
MultivaluedMapImpl sampleHeaders = new MultivaluedMapImpl()
sampleHeaders.add("name", "test")
clientResponse.getHeaders() >> sampleHeaders
HttpResponse httpResponse = new HttpClientResponse(clientResponse,null,null)
restClient.executeWithLoadBalancer(_ as HttpRequest) >> httpResponse
旁注,要模拟 ClientResponse,您需要在类路径中添加 objenesis。