|
| 1 | +package com.baeldung.completablefuture; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | + |
| 5 | +import java.util.concurrent.CompletableFuture; |
| 6 | +import java.util.concurrent.ExecutionException; |
| 7 | +import java.util.concurrent.Executors; |
| 8 | +import java.util.concurrent.Future; |
| 9 | +import java.util.stream.Collectors; |
| 10 | +import java.util.stream.Stream; |
| 11 | + |
| 12 | +import static org.junit.Assert.assertEquals; |
| 13 | +import static org.junit.Assert.assertTrue; |
| 14 | + |
| 15 | +public class CompletableFutureTest { |
| 16 | + |
| 17 | + @Test |
| 18 | + public void whenRunningCompletableFutureAsynchronously_thenGetMethodWaitsForResult() throws InterruptedException, ExecutionException { |
| 19 | + |
| 20 | + Future<String> completableFuture = calculateAsync(); |
| 21 | + |
| 22 | + String result = completableFuture.get(); |
| 23 | + assertEquals("Hello", result); |
| 24 | + |
| 25 | + } |
| 26 | + |
| 27 | + public Future<String> calculateAsync() throws InterruptedException { |
| 28 | + CompletableFuture<String> completableFuture = new CompletableFuture<>(); |
| 29 | + |
| 30 | + Executors.newCachedThreadPool().submit(() -> { |
| 31 | + Thread.sleep(500); |
| 32 | + completableFuture.complete("Hello"); |
| 33 | + return null; |
| 34 | + }); |
| 35 | + |
| 36 | + return completableFuture; |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void whenRunningCompletableFutureWithResult_thenGetMethodReturnsImmediately() throws InterruptedException, ExecutionException { |
| 41 | + |
| 42 | + Future<String> completableFuture = CompletableFuture.completedFuture("Hello"); |
| 43 | + |
| 44 | + String result = completableFuture.get(); |
| 45 | + assertEquals("Hello", result); |
| 46 | + |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void whenCreatingCompletableFutureWithSupplyAsync_thenFutureReturnsValue() throws ExecutionException, InterruptedException { |
| 51 | + |
| 52 | + CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Hello"); |
| 53 | + |
| 54 | + assertEquals("Hello", future.get()); |
| 55 | + |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void whenAddingThenAcceptToFuture_thenFunctionExecutesAfterComputationIsFinished() throws ExecutionException, InterruptedException { |
| 60 | + |
| 61 | + CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> "Hello"); |
| 62 | + |
| 63 | + CompletableFuture<Void> future = completableFuture.thenAccept(s -> System.out.println("Computation returned: " + s)); |
| 64 | + |
| 65 | + future.get(); |
| 66 | + |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void whenAddingThenRunToFuture_thenFunctionExecutesAfterComputationIsFinished() throws ExecutionException, InterruptedException { |
| 71 | + |
| 72 | + CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> "Hello"); |
| 73 | + |
| 74 | + CompletableFuture<Void> future = completableFuture.thenRun(() -> System.out.println("Computation finished.")); |
| 75 | + |
| 76 | + future.get(); |
| 77 | + |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void whenAddingThenApplyToFuture_thenFunctionExecutesAfterComputationIsFinished() throws ExecutionException, InterruptedException { |
| 82 | + |
| 83 | + CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> "Hello"); |
| 84 | + |
| 85 | + CompletableFuture<String> future = completableFuture.thenApply(s -> s + " World"); |
| 86 | + |
| 87 | + assertEquals("Hello World", future.get()); |
| 88 | + |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void whenUsingThenCompose_thenFuturesExecuteSequentially() throws ExecutionException, InterruptedException { |
| 93 | + |
| 94 | + CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> "Hello") |
| 95 | + .thenCompose(s -> CompletableFuture.supplyAsync(() -> s + " World")); |
| 96 | + |
| 97 | + assertEquals("Hello World", completableFuture.get()); |
| 98 | + |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void whenUsingThenCombine_thenWaitForExecutionOfBothFutures() throws ExecutionException, InterruptedException { |
| 103 | + |
| 104 | + CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> "Hello") |
| 105 | + .thenCombine(CompletableFuture.supplyAsync(() -> " World"), |
| 106 | + (s1, s2) -> s1 + s2); |
| 107 | + |
| 108 | + assertEquals("Hello World", completableFuture.get()); |
| 109 | + |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void whenUsingThenAcceptBoth_thenWaitForExecutionOfBothFutures() throws ExecutionException, InterruptedException { |
| 114 | + |
| 115 | + CompletableFuture.supplyAsync(() -> "Hello") |
| 116 | + .thenAcceptBoth(CompletableFuture.supplyAsync(() -> " World"), |
| 117 | + (s1, s2) -> System.out.println(s1 + s2)); |
| 118 | + |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void whenFutureCombinedWithAllOfCompletes_thenAllFuturesAreDone() throws ExecutionException, InterruptedException { |
| 123 | + |
| 124 | + CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> "Hello"); |
| 125 | + CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> "Beautiful"); |
| 126 | + CompletableFuture<String> future3 = CompletableFuture.supplyAsync(() -> "World"); |
| 127 | + |
| 128 | + CompletableFuture<Void> combinedFuture = CompletableFuture.allOf(future1, future2, future3); |
| 129 | + |
| 130 | + // ... |
| 131 | + |
| 132 | + combinedFuture.get(); |
| 133 | + |
| 134 | + assertTrue(future1.isDone()); |
| 135 | + assertTrue(future2.isDone()); |
| 136 | + assertTrue(future3.isDone()); |
| 137 | + |
| 138 | + String combined = Stream.of(future1, future2, future3) |
| 139 | + .map(CompletableFuture::join) |
| 140 | + .collect(Collectors.joining(" ")); |
| 141 | + |
| 142 | + assertEquals("Hello Beautiful World", combined); |
| 143 | + |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + public void whenFutureThrows_thenHandleMethodReceivesException() throws ExecutionException, InterruptedException { |
| 148 | + |
| 149 | + String name = null; |
| 150 | + |
| 151 | + // ... |
| 152 | + |
| 153 | + CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> { |
| 154 | + if (name == null) { |
| 155 | + throw new RuntimeException("Computation error!"); |
| 156 | + } |
| 157 | + return "Hello, " + name; |
| 158 | + }).handle((s, t) -> s != null ? s : "Hello, Stranger!"); |
| 159 | + |
| 160 | + assertEquals("Hello, Stranger!", completableFuture.get()); |
| 161 | + |
| 162 | + } |
| 163 | + |
| 164 | + @Test(expected = ExecutionException.class) |
| 165 | + public void whenCompletingFutureExceptionally_thenGetMethodThrows() throws ExecutionException, InterruptedException { |
| 166 | + |
| 167 | + CompletableFuture<String> completableFuture = new CompletableFuture<>(); |
| 168 | + |
| 169 | + // ... |
| 170 | + |
| 171 | + completableFuture.completeExceptionally(new RuntimeException("Calculation failed!")); |
| 172 | + |
| 173 | + // ... |
| 174 | + |
| 175 | + completableFuture.get(); |
| 176 | + |
| 177 | + } |
| 178 | + |
| 179 | + @Test |
| 180 | + public void whenAddingThenApplyAsyncToFuture_thenFunctionExecutesAfterComputationIsFinished() throws ExecutionException, InterruptedException { |
| 181 | + |
| 182 | + CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> "Hello"); |
| 183 | + |
| 184 | + CompletableFuture<String> future = completableFuture.thenApplyAsync(s -> s + " World"); |
| 185 | + |
| 186 | + assertEquals("Hello World", future.get()); |
| 187 | + |
| 188 | + } |
| 189 | + |
| 190 | +} |
0 commit comments