K Ramesh
K Ramesh

Reputation: 21

code coverage of CompletableFuture.supplyAsync code

  1. I have 10 lines of code inside CompletableFuture.supplyAsync(() -> { }
  2. junit test case is skipping that 10 lines of code. How can i cover those 10 lines
    CompletableFuture.supplyAsync(() -> {

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


    }

Upvotes: 2

Views: 603

Answers (1)

Naman
Naman

Reputation: 32028

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.

This would give you an ease of testing the method separately without even calling the CompletableFuture.supplyAsync(...) line as used in the code.

For example, consider this abstraction

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

where the supplyingMyValue method exists such as

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.

Upvotes: 2

Related Questions