2

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?

Tahnik Mustasin
  • 2,216
  • 2
  • 18
  • 21
  • You can use `e.getCause() instanceof CustomException`. http://stackoverflow.com/questions/7526817/use-of-instance-of-in-java – alturkovic Feb 18 '17 at 14:25
  • Yeah but I was wondering if JUnit has some way of doing it. Because if I want to check against multiple exception it will soon get ugly. `assertThat(e).isInstanceOf(IllegalArgumentException.class)` for example doesn't work anymore. I think the API has changed. – Tahnik Mustasin Feb 18 '17 at 14:27

3 Answers3

3

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!"));
}
alturkovic
  • 990
  • 8
  • 31
  • Thanks. It is almost perfect. Can you please put `ExecutionException.class` inside expect and `CustomException.class` inside `isA` ? Because the `CustomException` is not thrown. `CustomException` is the cause of the exception. I will accept it as an answer right away :) – Tahnik Mustasin Feb 18 '17 at 14:41
  • If you want to catch the exception instead of letting your code fail, GhostCats answer is simpler than using the ExpectedException, my example might be better if you want your test to end by throwing an exception. – alturkovic Feb 18 '17 at 23:22
2

Easy, you can solve that using "built-in" things (Rules are nice, but not required here):

catch (ExecutionException e) {
  assertThat(e.getCause(), is(SomeException.class));

In other words: just fetch that cause; and then assert whatever needs to be asserted. (I am using assertThat and the is() matcher; see here for further reading )

GhostCat
  • 137,827
  • 25
  • 176
  • 248
0

If you just want to check the type of exception cause it can be done very easily

Suppose we are expecting SQLIntegrityConstraintViolationException to be the cause so when we have Exception object we can check the class type of its cause for eg:

Exception ex = Assertion.assertThrows(RuntimeExcpetion.class, () -> someMethodWhichWillThrowRuntimeException());
Assertions.assertEquals(SQLIntegrityConstraintViolationException.class, ex.getCause().getClass());

Hope this helps!!

Rituraj Sharma
  • 111
  • 1
  • 5