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();
}
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();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.