为了账号安全,请及时绑定邮箱和手机立即绑定

为 Future 回调编写 junit 测试用例

为 Future 回调编写 junit 测试用例

阿波罗的战车 2023-08-04 14:50:06
有人可以帮助为下面的代码部分(尤其是添加回调部分)编写 JUnit 测试吗?我不知道如何使用回调为 Listenablefuture 编写单元测试用例    private void handleResponse(final ListenableFuture<UserRecordResult> response, CompletableFuture future) {        Futures.addCallback(response, new FutureCallback<UserRecordResult>() {            @Override            public void onFailure(@Nonnull Throwable throwable) {                future.completeExceptionally(new Exception("Fail to put record" + throwable.getMessage()));            }            @Override            public void onSuccess(UserRecordResult result) {                if(result.isSuccessful()) {                    future.complete(true);                } else {                    future.completeExceptionally(new Exception("Fail to put record"));                }            }        });    }
查看完整描述

1 回答

?
杨__羊羊

TA贡献1943条经验 获得超7个赞

您可以通过存根ListeneableFuture并按CompletableFuture以下方式进行测试:


@Test

public void completeExceptionallyOnCallbackFailure() {

    ListenableFuture<UserRecordResult> failureResponse = Futures.immediateFailedFuture(new RuntimeException());

    CompletableFuture future = new CompletableFuture();


    handleResponse(failureResponse, future);


    assertThat(future.isCompletedExceptionally(), equalTo(true));

}


@Test

public void completeExceptionallyIfUserRecordResultIsNotSuccessful() {

    UserRecordResult mockResult = Mockito.mock(UserRecordResult.class);

    Mockito.when(mockResult.isSuccessful()).thenReturn(false);


    ListenableFuture<UserRecordResult> failureResponse = Futures.immediateFuture(mockResult);

    CompletableFuture future = new CompletableFuture();


    handleResponse(failureResponse, future);


    assertThat(future.isCompletedExceptionally(), equalTo(true));

}


@Test

public void completeSuccessfully() throws ExecutionException, InterruptedException {

    UserRecordResult mockResult = Mockito.mock(UserRecordResult.class);

    Mockito.when(mockResult.isSuccessful()).thenReturn(true);


    ListenableFuture<UserRecordResult> successResponse = Futures.immediateFuture(mockResult);

    CompletableFuture future = new CompletableFuture();


    handleResponse(successResponse, future);


    assertThat(future.get(), equalTo(true));

}

此外,您可以使用以下方法断言实际的错误消息assertj:


@Test

public void verifyMessageIfCompletedExceptionallyAfterUserRecordResultIsNotSuccessful() {

    UserRecordResult mockResult = Mockito.mock(UserRecordResult.class);

    Mockito.when(mockResult.isSuccessful()).thenReturn(false);


    ListenableFuture<UserRecordResult> failureResponse = Futures.immediateFuture(mockResult);

    CompletableFuture future = new CompletableFuture();


    Assertions.assertThatThrownBy(() -> {

        handleResponse(failureResponse, future);

        future.get();

    })

    .isInstanceOf(Exception.class)

    .hasMessage("java.lang.Exception: Fail to put record");

}


查看完整回答
反对 回复 2023-08-04
  • 1 回答
  • 0 关注
  • 183 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信