The following method is providing instances of java.net.http.HttpClient:
<code>@CompileStatic
private static HttpClient getClient() {
return HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_2)
.connectTimeout(Duration.ofSeconds(5))
.build()
}
</code>
<code>@CompileStatic
private static HttpClient getClient() {
return HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_2)
.connectTimeout(Duration.ofSeconds(5))
.build()
}
</code>
@CompileStatic
private static HttpClient getClient() {
return HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_2)
.connectTimeout(Duration.ofSeconds(5))
.build()
}
These instances are used to perform REST-api calls in the same class to a third party site, like this:
<code>final HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString())
</code>
<code>final HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString())
</code>
final HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString())
I would like to mock these calls so that I can specify the return value, but I cannot figure out how to do it without refactoring the code (which is not an option).
The following does not work (not on HttpClientImpl either) because instance cannot be created.
<code>HttpClient client GroovySpy(global: true)
HttpClient client GroovyMock(global: true)
</code>
<code>HttpClient client GroovySpy(global: true)
HttpClient client GroovyMock(global: true)
</code>
HttpClient client GroovySpy(global: true)
HttpClient client GroovyMock(global: true)
Overriding the getClient() method is really not an option since there are many injected dependencies that need to be handled.