in my class i have i class-field, which i’am verfing, that the method of this class-field is invoked three times. Now i moved the logic to “CompletableFuture.sypplyAsync” approach and it does not work anymore:
My Test fails, and it says, that it will be not invoked, what i’am expecting.
My Class:
public class StartProcessService {
private final RuntimeService runtimeService;
public void handleInExkassodatenRequest(MyRequest request) {
request.getDaten().forEach(entity -> {
CompletableFuture.supplyAsync(() -> selectRequiredProcess(entity))
.thenAccept(caseId -> {
log.info("Case started ");
})
.exceptionally(err -> {
return null;
});
});
}
private CompletableFuture<String> selectRequiredProcess(InExkassodaten entity) {
startNewProcess(entity);
return CompletableFuture.completedFuture(entity.getCaseId());
}
private void startNewProcess(InExkassodaten inExkassodaten) {
ProcessInstance processInstance = runtimeService
.createProcessInstanceByKey("MY_KEY")
.execute();
}
}
and my old test-method looks like this:
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
class MyServiceTest {
@InjectMocks
private StartProcessService startProcessService;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private RuntimeService runtimeService;
@Test
void startProcesses() {
startProcessService.handleInExkassodatenRequest(InExkassodatenFactory.createValidTestData());
verify(runtimeService, times(3)).createProcessInstanceByKey(anyString());
}
}
previously checked whether runtimeService.createProcessInstanceByKey was called.
But with introduction of CompletableFutur.supplyAsync in fails with:
Wanted but not invoked: runtimeService.createProcessInstanceByKey(
);
-> at StartProcessServiceTest.startProcesses(StartProcessServiceTest.java:52)
Actually, there were zero interactions with this mock.
How do i need to redesign code my production or testcode to test it with CompleableFuture?