Async processes in Java Spring
To test these things the first thing I usually do is wire in a sameThreadExecutor; if you don't want a dependency you'll have to write your own but it's just an implementation of ExecutorService that calls its callable immediately and returns a completed future.

The aim is to replace the async part of your code with something synchronous. The mocking could be done with Mockito, it's just very unwieldy. You would have to mock the task executor with a doAnswer:

Mockito.when(taskExecutor.call(any())).thenAnswer((invocation) -&gt; ... ugly work goes here ...)


For safety you might want to add timeout parameters to the call() method and handle timeout exceptions (cancel the timed out future).