使用Mockito发送请求时模拟WebClient

时间:2020-01-08 15:09:36

标签: java junit mockito

我有一个向服务器发出请求以检查状态的端点。我将WebClientvertex一起使用。我创建了这样的客户。

WebClient client = WebClient.create(vertx);

,请求如下所示

client.get(check.url).send(ar -> {
    HttpResponse<Buffer> resp = ar.result();
    if (resp != null) {
        check.response = resp.bodyAsString();
    } else {
        check.response = "";
    }
    logger.debug("Received response from app: {} with result:{} on url:{} : {}",
            check.appName, check.result, check.url, check.response);
    check.result.complete(ar.succeeded());
});

我的问题是客户端尝试发送请求时。 编辑:我想模拟该client.send()并返回状态为200或400的WebClient。请注意,我无法访问此WebClient,因为它是根据我要测试的方法声明的。

我正在尝试修复此测试。 测试

@Test
public void testWongUrlsAndNormalMode() throws Exception{
    when(healthCheckEndpoint.configuration.getMode()).thenReturn(HealthCheckerConfiguration.HealthCheckMode.NORMAL);
    when(healthCheckEndpoint.configuration.getHealthCheckUrls()).thenReturn(urls);

    Response a = healthCheckEndpoint.checkApplications(); //here is when the client.get(url).send() returns nullPointerException

    assertEquals(a.getStatus(), 500);
    assertEquals(a.getEntity(), "normal mode no app urls");
}

但是如上所述,当WebClient尝试发送请求时,出现空指针异常。 我也尝试过模拟WebClient,但是我不能模拟send()方法,因为它是一个无效方法

0 个答案:

没有答案