为 Future 回调编写 junit 测试用例

有人可以帮助为下面的代码部分(尤其是添加回调部分)编写 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"));

                }

            }

        });

    }


阿波罗的战车
浏览 184回答 1
1回答

杨__羊羊

您可以通过存根ListeneableFuture并按CompletableFuture以下方式进行测试:@Testpublic void completeExceptionallyOnCallbackFailure() {&nbsp; &nbsp; ListenableFuture<UserRecordResult> failureResponse = Futures.immediateFailedFuture(new RuntimeException());&nbsp; &nbsp; CompletableFuture future = new CompletableFuture();&nbsp; &nbsp; handleResponse(failureResponse, future);&nbsp; &nbsp; assertThat(future.isCompletedExceptionally(), equalTo(true));}@Testpublic void completeExceptionallyIfUserRecordResultIsNotSuccessful() {&nbsp; &nbsp; UserRecordResult mockResult = Mockito.mock(UserRecordResult.class);&nbsp; &nbsp; Mockito.when(mockResult.isSuccessful()).thenReturn(false);&nbsp; &nbsp; ListenableFuture<UserRecordResult> failureResponse = Futures.immediateFuture(mockResult);&nbsp; &nbsp; CompletableFuture future = new CompletableFuture();&nbsp; &nbsp; handleResponse(failureResponse, future);&nbsp; &nbsp; assertThat(future.isCompletedExceptionally(), equalTo(true));}@Testpublic void completeSuccessfully() throws ExecutionException, InterruptedException {&nbsp; &nbsp; UserRecordResult mockResult = Mockito.mock(UserRecordResult.class);&nbsp; &nbsp; Mockito.when(mockResult.isSuccessful()).thenReturn(true);&nbsp; &nbsp; ListenableFuture<UserRecordResult> successResponse = Futures.immediateFuture(mockResult);&nbsp; &nbsp; CompletableFuture future = new CompletableFuture();&nbsp; &nbsp; handleResponse(successResponse, future);&nbsp; &nbsp; assertThat(future.get(), equalTo(true));}此外,您可以使用以下方法断言实际的错误消息assertj:@Testpublic void verifyMessageIfCompletedExceptionallyAfterUserRecordResultIsNotSuccessful() {&nbsp; &nbsp; UserRecordResult mockResult = Mockito.mock(UserRecordResult.class);&nbsp; &nbsp; Mockito.when(mockResult.isSuccessful()).thenReturn(false);&nbsp; &nbsp; ListenableFuture<UserRecordResult> failureResponse = Futures.immediateFuture(mockResult);&nbsp; &nbsp; CompletableFuture future = new CompletableFuture();&nbsp; &nbsp; Assertions.assertThatThrownBy(() -> {&nbsp; &nbsp; &nbsp; &nbsp; handleResponse(failureResponse, future);&nbsp; &nbsp; &nbsp; &nbsp; future.get();&nbsp; &nbsp; })&nbsp; &nbsp; .isInstanceOf(Exception.class)&nbsp; &nbsp; .hasMessage("java.lang.Exception: Fail to put record");}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java