Java 异步方法单元测试

2022-08-14  本文已影响0人  redexpress

对于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();
}

如果你想运行本示例代码的,需要加上这个依赖

上一篇 下一篇

猜你喜欢

热点阅读