简体   繁体   中英

Write junit test cases for the Future callback

Can someone help to write JUnit test for below section of code especially the add callback part? I am not sure how to write unit test cases for the Listenablefuture with the callback

    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"));
                }
            }
        });
    }

You can test by stubbing ListeneableFuture and CompletableFuture in following manner:

@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));
}

Also, you can assert actual error messages by using 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");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM