I have this code:
CompletableFuture<SomeClass> future = someInstance.getSomething(-902);
try {
future.get(15, TimeUnit.SECONDS);
fail("Print some error");
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
// Here I want to check if e.getCause() matches some exception
} catch (TimeoutException e) {
e.printStackTrace();
}
So when a ExecutionException is thrown, it is thrown by another exception in another class. I want to check if the original exception that cause ExecutionException matches some custom exception that I created. How do I achieve that with JUnit?
Use ExpectedException
like this:
@Rule
public final ExpectedException expectedException = ExpectedException.none();
@Test
public void testExceptionCause() throws Exception {
expectedException.expect(ExecutionException.class);
expectedException.expectCause(isA(CustomException.class));
throw new ExecutionException(new CustomException("My message!"));
}