I am writing unit tests for a class which has a latch to block the execute method. Class structure is something like this :
class Sample {
private CountDownLatch latch;
public void execute(String data) {
latch = new CountDownLatch(1);
// business logic
latch.await();
}
public void onSuccess(String data) {
if (latch != null) {
latch.countDown();
}
}
}
To test the execute method i have added mock for all dependencies, but since the latch is initialized in the execute method, I am unable to mock that. So to write the unit tests for this, do I need to spawn a new thread in the test to call the countdown on latch or is there any other way to test this ?