📜  immediateFuture 测试类 - 任何代码示例

📅  最后修改于: 2022-03-11 14:56:10.398000             🧑  作者: Mango

代码示例1
@Test
public void completeExceptionallyOnCallbackFailure() {
    ListenableFuture 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 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 successResponse = Futures.immediateFuture(mockResult);
    CompletableFuture future = new CompletableFuture();

    handleResponse(successResponse, future);

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