Reputation: 1868
I tried looking this up, but couldn't quite understand the examples I found. I have a class with a Spring AfterPropertiesSet()
method that calls 2 other methods to spin off asynch calls. I'm at a loss how as to to unit test them. The method startProcessingThread()
looks like this:
void startProcessingThread(executor) {
CompletableFuture.runAsync(() -> {
Path filePath = null;
do {
filePath = retrieveFromFileQueue();
submitFileForProcessing(filePath);
} while (keepRunning);
}, executor)
.handle((theVoid, exception) -> {
if (exception != null) {
LOG.error("Error submitting file for processing: " + exception);
}
return null;
});
}
I don't want to rewrite the CompletableFuture
in the test method (do I?). So I figured I need to call startProcessingThread()
, using Mocks (from Mockito) inside the methods that need them (retrieveFromFileQueue()
and submitFileForProcessing()
). But what about the CompletableFuture
itself? Should I mock that? I'm sorry, but I'm really confused...
Upvotes: 2
Views: 10097
Reputation: 30820
You should avoid non deterministic logic in your test and so I would advise to avoid sleep. Instead you can use Mockito's "when" instead of "verify" and wait until the action was performed:
final xyz[] result = new[]{null};
final CountDownLatch latch = new CountDownLatch(1);
when(submitFileForProcessing(...).thenAnswer((Answer<Lwm2mCommand>) invocationOnMock ->
{
result[0] = invocationOnMock.getArgument(index); // if you need to check the values passed to your method call
latch.countDown();
}
// Call your method
assertTrue(latch.await(60L, TimeUnit.SECONDS));
// If needed check the parameters passed to the mocked method
One more tip: I wouldn't mock the actual class under test - instead I would mock one of the dependencies used.
Upvotes: 2