Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExpectedException cause of the cause?

I'm trying to verify that all my exceptions are correct. Because values are wrapped in CompletableFutures, the exception thrown is ExecutionException with cause being the exception that I would normally check. Quick example:

void foo() throws A {
  try {
    bar();
  } catch B b {
    throw new A(b);
  }
}

So foo() translates exception thrown by bar(), and all of that is done inside CompletableFutures and AsyncHandlers (I won't copy the whole code, it's just for reference)

In my unit tests I'm making bar() throw an exception and want to check that it's translated correctly when calling foo():

Throwable b = B("bleh");
when(mock.bar()).thenThrow(b);
ExpectedException thrown = ExpectedException.none();
thrown.expect(ExecutionException.class);
thrown.expectCause(Matchers.allOf(
                instanceOf(A.class),
                having(on(A.class).getMessage(),
                        CoreMatchers.is("some message here")),
        ));

So far so good, but I also want to verify that cause of exception A is exception B and having(on(A.class).getCause(), CoreMatchers.is(b)) causes CodeGenerationException --> StackOverflowError

TL;DR: How do I get cause of cause of expected exception?

like image 290
Amir Avatar asked Nov 25 '16 16:11

Amir


1 Answers

Maybe you should try with the simple hasProperty Matcher, in order to isolate the problem:

thrown.expectCause(allOf(
                    instanceOf(A.class),
                    hasProperty("message", is("some message here")),
        ));
like image 92
Ruben Avatar answered Sep 28 '22 06:09

Ruben