I'm trying to test my code which uses the new Java 11 java.net.http.HttpClient.
In my production code I have something like this:
HttpClient httpClient = ... (gets injected)
HttpRequest request = HttpRequest.newBuilder().uri(URI.create("http://localhost:1234"))
.POST(HttpRequest.BodyPublishers.ofByteArray("example".getBytes()))
.build();
return httpClient.send(request, HttpResponse.BodyHandlers.ofByteArray());
And in my test I mock the HttpClient and so get the java.net.http.HttpRequest. How do I get/test its request body (= my "example")? I can call request.bodyPublisher() to get a HttpRequest.BodyPublisher, but then I'm stuck.
- I've tried to cast it to
jdk.internal.net.http.RequestPublishers.ByteArrayPublisher(which it actually is), but it won't compile because the corresponding package is not exported by the module. - I've checked the available methods in the
HttpRequest.BodyPublisher-interface (.contentLength(),.subscribe(subscriber)) but I guess it's not possible with them. - I've tried to just create a new
BodyPublisherand compare them using.equals(), but there is no real implementation of it and so the comparison was always false.