6

There is a code written by someone who had called completable future to do an activity that is a call and forget i.e. output is not looked after (no get() call).

CompletableFuture.runAsync(() -> {
            List<ActivityListTO> activities = auditCount(requestType, oldLimit, rule, newLimit);
            few mores lines here. 
...
, executor);

I was unit testing this but unable to execute the code inside of async.
I cannot static mock it as I need to execute the code inside it. I tried passing the thread pool instance in my testcase but it did not go.

Could you provide me some hints on how can I unit test the code inside async?

Didier L
  • 18,905
  • 10
  • 61
  • 103
Learner
  • 176
  • 4
  • 16
  • Simplest would probably be to wrap the `runAsync()` code in a separate service, and mock that service. This way you could even test the lambda itself independently. – Didier L Aug 21 '19 at 13:19
  • Yes, This is one of the option but There has been guideline not to change the existing code. any other option do you see to get this working? – Learner Aug 22 '19 at 06:32
  • 1
    You can inject a custom Executor Instance and then do something like described in the thread: https://stackoverflow.com/questions/631598/how-to-use-junit-to-test-asynchronous-processes – MemLeak Dec 05 '19 at 10:25

0 Answers0