Hello! I have a Parent workflow with a child workflow that runs asynchronous, like:
// some code
...
ChildWF child = Workflow.newChildWorkflowStub(ChildWF.class,
ChildWorkflowOptions.newBuilder()
.setParentClosePolicy(ParentClosePolicy.PARENT_CLOSE_POLICY_ABANDON)
.setCronSchedule(cronSched)
.setWorkflowId(workflowId)
.build());
Async.procedure(child::childWFMethod, param);
try {
Promise<WorkflowExecution> childPromise = Workflow.getWorkflowExecution(child);
childPromise.get();
} catch (ChildWorkflowFailure ex) {
if (ex.getCause() instanceof WorkflowExecutionAlreadyStarted) {
logger.error("whatever");
}
} catch (Exception ex) {
logger.error("whatever");
}
// end parent wf
Then I want to test the failure of the child wf, so in my test I tried to mock the Workflow.getWorkflowExecution(child) to throw an exception, but I am not able to make it work.
// previous mocked activities definition, TestWorkflowExtension, ActivityStubFactory and so...
worker.registerWorkflowImplementationFactory(ParentWF.class, () -> {
var workflow = new ParentWFImpl(params);
this.factory.inject(workflow, workflow.getClass());
return workflow;
});
worker.registerWorkflowImplementationFactory(ChildWF.class, () -> {
var childworkflow = mock(ChildWF.class);
Mockito.doNothing().when(childworkflow).childWFMethod(Mockito.any());
this.factory.inject(childworkflow, ChildWF.class);
return childworkflow;
});
var parentWorkflow = testEnv.getWorkflowClient().newWorkflowStub(ParentWF.class,
WorkflowOptions.newBuilder().setTaskQueue(worker.getTaskQueue()).setWorkflowId("test").build());
RuntimeException runtimeException = new RuntimeException("test error");
try (MockedStatic<Workflow> wf = Mockito.mockStatic(Workflow.class)) {
wf.when(() -> Workflow.getWorkflowExecution(childWF)).thenThrow(runtimeException);
Assertions.assertThatExceptionOfType(RuntimeException.class).isThrownBy(() -> Workflow.getWorkflowExecution(childWF));
}
testEnv.start();
Assertions.assertThatCode(() -> parentWorkflow.parentWFMethod(someParam)).doesNotThrowAnyException();
When I run the test, I have it green, like succesfull, but when I launch the coverage or debug it, I see that never enter in the exceptions part, that is what I want to test.
Is there something I am missing, thanks.