简体   繁体   English

Java8 UnitTesting CompletableFuture异常

[英]Java8 UnitTesting CompletableFuture Exception

I'm using CompletableFuture Java8 class to make network calls asynchronously in Java, specifically using the supplyAsync() method. 我正在使用CompletableFuture Java8类在Java中异步进行网络调用,特别是使用supplyAsync()方法。 It's working great. 效果很好。 I've figured out that I can unit test the "happy path" scenarios using CompletableFuture.completedFuture() . 我发现我可以使用CompletableFuture.completedFuture()对“快乐之路”场景进行单元测试。 What I'm trying to figure out is how to unit test (if possible) the cases where an exception (ie CompletionException , InterruptedException , or ExecutionException ) is thrown during the async task. 我要弄清楚的是如何对异步任务期间引发异常(例如CompletionExceptionInterruptedExceptionExecutionException )的情况进行单元测试(如果可能)。

https://www.baeldung.com/java-completablefuture was a starting point and useful source but doesn't address this question. https://www.baeldung.com/java-completablefuture是一个起点和有用的资源,但并未解决此问题。

My first approach does not compile: 我的第一种方法无法编译:

final CompletableFuture<ResponseType> completableFutureException = CompletableFuture.completedFuture(new InterruptedException());

My second approach predictably generates a ClassCastException at runtime: final CompletableFuture completableFutureException = CompletableFuture.completedFuture(new InterruptedException()); 我的第二种方法可以在运行时生成ClassCastExceptionfinal CompletableFuture completableFutureException = CompletableFuture.completedFuture(new InterruptedException());

java.lang.ClassCastException: java.lang.InterruptedException cannot be cast to ResponseType

It also sounds like the CompletableFuture<U> newIncompleteFuture() method in Java9 might help - alas, we are stuck on Java8 for the moment. 听起来CompletableFuture<U> newIncompleteFuture()CompletableFuture<U> newIncompleteFuture()方法可能会有所帮助-a,我们暂时还停留在Java8上。 If Java9 would help me here, I'd still appreciate knowing. 如果Java9在这里对我有帮助,我仍然很高兴知道。

As I've said, I'd like to figure out if there is a reasonable way to test this in Java8 (preferably without using PowerMock, as we've had trouble getting that to play nicely with Gradle). 就像我说过的,我想弄清楚是否有一种合理的方法可以在Java8中对此进行测试(最好不使用PowerMock,因为我们很难使它与Gradle完美兼容)。 If this really just isn't unit-testable, I can accept that and move on. 如果这真的不是可测试的,我可以接受并继续。

Java 9 introduced a new method failedFuture to meet this need. Java 9引入了一种新方法failFuture来满足这一需求。 You can easily create your own equivalent method if you're running Java 8. 如果您运行的是Java 8,则可以轻松创建自己的等效方法。

public static <T> CompletableFuture<T> failedFuture(Throwable ex) {
    // copied from Java 9 https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/concurrent/CompletableFuture.html#failedFuture(java.lang.Throwable)
    CompletableFuture<T> f = new CompletableFuture<>();
    f.completeExceptionally(ex);
    return f;
}

CompletableFuture<ResponseType> failure = failedFuture(new InterruptedException());

Alternatively you can use supplyAsync: 另外,您可以使用supplyAsync:

CompletableFuture<ResponseType> failure = CompletableFuture.supplyAsync(() -> {throw new InterruptedException();})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM