對于Java異步函數,需要等待結果返回,下面是一種比較簡單的方法。
@Test
public void testAsyncNetwork() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
WebClient.create(Vertx.vertx())
.get("httpbin.org", "/get")
.send()
.onSuccess(resp -> {
assertEquals(200, resp.statusCode());
})
.onFailure(err -> {
fail("something wrong");
})
.onComplete(event -> {
// 結束把latch置0
latch.countDown();
});
latch.await();
}
如果你想運行本示例代碼的,需要加上這個依賴。