i wanna test my “exceptionally”-Block of CompletableFuture.
I’ve already wrote a test, but is fails.
public CompletableFuture<Void> handleRequest(Request request) {
CompletableFuture<Void> result = null;
for (MyCustomer entity : request.getData()) {
result = CompletableFuture.supplyAsync(() -> selectStuff(entity))
.thenAccept(completableFutureCaseId ->
log.info("Sucess!!!", completableFutureCaseId.resultNow()))
.exceptionally(err -> {
sendErrorMessage(entity, err);
return null;
});
}
return result;
}
Inside of methode ‘selectStuff’ i’am throwing exception. My exceptionally-Block will be executed.
With a debugger, i can see in the beginning of execution of my method, that it jumps directly to “return result” and then it goes further to “supplyAsync”.
My Test looks like:
@Test
void failTostartInsertCase() {
when(myValidator.test(any())).thenReturn(false); //provoke 'throw exception' inside of 'selectStuff' method
CompletableFuture<Void> result = myService.handleRequest(validTestData);
result.join();
assertThat(result.isCompletedExceptionally()).isTrue(); //fails, because its false
}
How could i test it on isCompletedExceptionally=true?