[BAEL-9461] - Added code examples for difference between thenApply() and thenCompose()

This commit is contained in:
amit2103 2018-10-14 15:13:39 +05:30
parent 5a76e44ac1
commit ee5fe8faab
2 changed files with 20 additions and 0 deletions

View File

@ -184,5 +184,25 @@ public class CompletableFutureLongRunningUnitTest {
assertEquals("Hello World", future.get());
}
@Test
public void whenPassingTransformation_thenFunctionExecutionWithThenApply() throws InterruptedException, ExecutionException {
CompletableFuture<Integer> finalResult = compute().thenApply(s -> s + 1);
assertTrue(finalResult.get() == 11);
}
@Test
public void whenPassingPreviousStage_thenFunctionExecutionWithThenCompose() throws InterruptedException, ExecutionException {
CompletableFuture<Integer> finalResult = compute().thenCompose(this::computeAnother);
assertTrue(finalResult.get() == 20);
}
public CompletableFuture<Integer> compute(){
return CompletableFuture.supplyAsync(() -> 10);
}
public CompletableFuture<Integer> computeAnother(Integer i){
return CompletableFuture.supplyAsync(() -> 10 + i);
}
}