简体   繁体   English

CompletableFuture.supplyAsync 代码的代码覆盖率

[英]code coverage of CompletableFuture.supplyAsync code

  1. I have 10 lines of code inside CompletableFuture.supplyAsync(() -> { }我在CompletableFuture.supplyAsync(() -> { }中有 10 行代码
  2. junit test case is skipping that 10 lines of code. junit 测试用例跳过了那 10 行代码。 How can i cover those 10 lines我怎样才能覆盖这 10 行
    CompletableFuture.supplyAsync(() -> {

        // line 1
        // line 3
        ..
        ..
        ..
       // line 10


    }

Without the awareness of the actual test case.没有实际测试用例的意识。 What you can possibly do to improve the code is to abstract those 10 lines of code to a method with can be then be called from the supplier.您可以做的改进代码的方法是将这 10 行代码抽象为一个方法,然后可以从供应商处调用。

This would give you an ease of testing the method separately without even calling the CompletableFuture.supplyAsync(...) line as used in the code.这将使您可以轻松地单独测试该方法,甚至无需调用代码中使用的CompletableFuture.supplyAsync(...)行。

For example, consider this abstraction例如,考虑这个抽象

CompletableFuture.supplyAsync(() -> supplyingMyValue());

where the supplyingMyValue method exists such as存在supplyingMyValue方法的地方,例如

Value supplyingMyValue() {
    // perform some logic
    return new Value();
}

and now you can test this method independently .现在你可以独立测试这个方法了。

Note : The solution hereby would still not test the supplyAsync capability, but provide a way to test only the code invoked.注意:此解决方案仍然不会测试supplyAsync功能,而是提供一种仅测试调用代码的方法。

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

相关问题 使用 Mockito 测试 CompletableFuture.supplyAsync - Testing CompletableFuture.supplyAsync with Mockito ForkJoinPool在CompletableFuture.supplyAsync()中的行为 - Behaviour of ForkJoinPool in CompletableFuture.supplyAsync() CompletableFuture.supplyAsync() 没有 Lambda - CompletableFuture.supplyAsync() without Lambda 获取CompletableFuture.supplyAsync的结果 - Getting the result of a CompletableFuture.supplyAsync CompletableFuture.supplyAsync与新的CompletableFuture() - CompletableFuture.supplyAsync vs new CompletableFuture() 简单的 CompletableFuture.supplyAsync() 导致 IllegalMonitorStateException 错误 - Simple CompletableFuture.supplyAsync() leads to IllegalMonitorStateException error 使用CompletableFuture.supplyAsync返回多个值 - Returning multiple values with CompletableFuture.supplyAsync 如何将CompletableFuture.supplyAsync与PriorityBlockingQueue一起使用? - How do I use CompletableFuture.supplyAsync together with PriorityBlockingQueue? JDK8 CompletableFuture.supplyAsync 如何处理interruptedException - JDK8 CompletableFuture.supplyAsync how to deal with interruptedException 如何将completableFuture.supplyAsync()的返回类型分配给对象? - how to assign the return type of completableFuture.supplyAsync() to the object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM