BAEL-6579: fixing tests

This commit is contained in:
emanueltrandafir1993 2023-07-01 13:33:40 +02:00
parent dbbd2a0a12
commit 09c62a2d70
1 changed files with 15 additions and 9 deletions

View File

@ -1,8 +1,10 @@
package com.baeldung.concurrent.completablefuture.threadpool;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
@ -11,7 +13,7 @@ import org.junit.jupiter.api.Test;
public class CompletableFutureThreadPoolUnitTest {
@Test
void whenUsingNonAsync_thenUsesMainThread() {
void whenUsingNonAsync_thenUsesMainThread() throws ExecutionException, InterruptedException {
CompletableFuture<String> name = CompletableFuture.supplyAsync(() -> "Baeldung");
CompletableFuture<Integer> nameLength = name.thenApply(value -> {
@ -19,7 +21,7 @@ public class CompletableFutureThreadPoolUnitTest {
return value.length();
});
assertThat(nameLength).isCompletedWithValue(8);
assertThat(nameLength.get()).isEqualTo(8);
}
@Test
@ -32,7 +34,11 @@ public class CompletableFutureThreadPoolUnitTest {
return value.length();
});
assertThat(nameLength).isCompletedWithValue(8);
try {
assertThat(nameLength.get()).isEqualTo(8);
} catch (Exception e) {
fail(e.getMessage());
}
};
new Thread(test, "test-thread").start();
@ -40,7 +46,7 @@ public class CompletableFutureThreadPoolUnitTest {
}
@Test
void whenUsingAsync_thenUsesCommonPool() {
void whenUsingAsync_thenUsesCommonPool() throws ExecutionException, InterruptedException {
CompletableFuture<String> name = CompletableFuture.supplyAsync(() -> "Baeldung");
CompletableFuture<Integer> nameLength = name.thenApplyAsync(value -> {
@ -48,11 +54,11 @@ public class CompletableFutureThreadPoolUnitTest {
return value.length();
});
assertThat(nameLength).isCompletedWithValue(8);
assertThat(nameLength.get()).isEqualTo(8);
}
@Test
void whenUsingAsync_thenUsesCustomExecutor() {
void whenUsingAsync_thenUsesCustomExecutor() throws ExecutionException, InterruptedException {
Executor testExecutor = Executors.newFixedThreadPool(5);
CompletableFuture<String> name = CompletableFuture.supplyAsync(() -> "Baeldung");
@ -61,11 +67,11 @@ public class CompletableFutureThreadPoolUnitTest {
return value.length();
}, testExecutor);
assertThat(nameLength).isCompletedWithValue(8);
assertThat(nameLength.get()).isEqualTo(8);
}
@Test
void whenOverridingDefaultThreadPool_thenUsesCustomExecutor() {
void whenOverridingDefaultThreadPool_thenUsesCustomExecutor() throws ExecutionException, InterruptedException {
CompletableFuture<String> name = CustomCompletableFuture.supplyAsync(() -> "Baeldung");
CompletableFuture<Integer> nameLength = name.thenApplyAsync(value -> {
@ -73,7 +79,7 @@ public class CompletableFutureThreadPoolUnitTest {
return value.length();
});
assertThat(nameLength).isCompletedWithValue(8);
assertThat(nameLength.get()).isEqualTo(8);
}
private static void printCurrentThread() {