Refactor CompletableFutureTest

This commit is contained in:
Grzegorz Piwowarek 2016-08-15 23:20:39 +02:00
parent 5195a4310e
commit 0bb0966723
1 changed files with 5 additions and 11 deletions

View File

@ -1,5 +1,7 @@
package com.baeldung.completablefuture; package com.baeldung.completablefuture;
import org.junit.Test;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
@ -7,15 +9,13 @@ import java.util.concurrent.Future;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
import org.junit.Test;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
public class CompletableFutureTest { public class CompletableFutureTest {
@Test @Test
public void whenRunningCompletableFutureAsyncronously_thenGetMethodWaitsForResult() throws InterruptedException, ExecutionException { public void whenRunningCompletableFutureAsynchronously_thenGetMethodWaitsForResult() throws InterruptedException, ExecutionException {
Future<String> completableFuture = calculateAsync(); Future<String> completableFuture = calculateAsync();
@ -144,7 +144,7 @@ public class CompletableFutureTest {
} }
@Test @Test
public void whenFutureThrows_thenHandleMethodRecievesException() throws ExecutionException, InterruptedException { public void whenFutureThrows_thenHandleMethodReceivesException() throws ExecutionException, InterruptedException {
String name = null; String name = null;
@ -155,13 +155,7 @@ public class CompletableFutureTest {
throw new RuntimeException("Computation error!"); throw new RuntimeException("Computation error!");
} }
return "Hello, " + name; return "Hello, " + name;
}).handle((s, t) -> { }).handle((s, t) -> s != null ? s : "Hello, Stranger!");
if (s != null) {
return s;
} else {
return "Hello, Stranger!";
}
});
assertEquals("Hello, Stranger!", completableFuture.get()); assertEquals("Hello, Stranger!", completableFuture.get());