新しい Java 11 を使用するコードをテストしようとしていますjava.net.http.HttpClient
。
私のプロダクションコードには、次のようなものがあります。
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());
そして、私のテストでは、 をモックしてHttpClient
を取得しjava.net.http.HttpRequest
ます。リクエストボディ (= my ) を取得/テストするにはどうすればよい"example"
ですか? 電話request.bodyPublisher()
して を取得できますHttpRequest.BodyPublisher
が、行き詰まります。
- キャストしようとしましたが
jdk.internal.net.http.RequestPublishers.ByteArrayPublisher
(実際にはそうです)、対応するパッケージがモジュールによってエクスポートされていないため、コンパイルされません。 HttpRequest.BodyPublisher
-interface (.contentLength()
, )で使用可能なメソッドを確認しましたが、.subscribe(subscriber)
それらでは不可能だと思います。- 新しいものを作成し
BodyPublisher
て を使用して比較しようとしまし.equals()
たが、実際の実装がないため、比較は常に false でした。