-1

This test fails because I get CompletionException exception but the method throws IllegalArgumentException. So the actual exception wrapped into CompletionException. How can I extract the exception that is wrapped into CompletionException? I tried exceptionally, handle etc but does not give me what I want. I get the exception as java.util.concurrent.CompletionException: java.lang.IllegalArgumentException: some message.

@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "some message")
public void someTest() {
    CompletableFuture.runAsync(() -> someMethodThrowsException()).join();
}
James
  • 441
  • 2
  • 6
  • 9

1 Answers1

3

This is what you can do in order to get the original exception thrown by your function :

public class MyTest {

    @Test(expected = IllegalArgumentException.class)
    public void myTest() throws Throwable {
        try {
            CompletableFuture.runAsync(() -> myException()).join();
        } catch (CompletionException e) {
            throw e.getCause();
        }
    }

    public static void myException() {
        throw new IllegalArgumentException();
    }
}
zpavel
  • 951
  • 5
  • 11