Unit Test for Signal which is send from a Workflow

Hello!
I am implementing an approval approcess using Temporal. There are two Workflows, one for creating a request (CreateResourceAccessWFImpl) which waits unit a signal is sent and other for sending Signal(UpdateResourceAccessWFImpl) to the CreateRequest workflow (the first workflow) to approve or reject.

public class UpdateResourceAccessWFImpl implements UpdateResourceAccessWF {
public void updateResource(String workflowId) {
...
ExternalWorkflowStub callRespondWorkflow = Workflow.newUntypedExternalWorkflowStub(workflowId);
    callRespondWorkflow.signal("updateStatus", approvalState, updatedDate);
...
}
}

How do I write a JUnit for this?

The Junit for the createRequest looks something like this -

@RegisterExtension
  public TestWorkflowExtension testWorkflowRule =
      TestWorkflowExtension.newBuilder()
          .registerWorkflowImplementationTypes(CreateResourceAccessWFImpl.class)
          .setDoNotStart(true)
          .build();

  @Test
  void createResourceAccessRequestAndSendApproveSignal(
      TestWorkflowEnvironment testEnv,
      Worker worker,
      CreateResourceAccessWF createResourceAccessWF) {
    ...
    ResourceAccessActivity resourceAccessRepository = mock(ResourceAccessActivity.class);
    worker.registerActivitiesImplementations(resourceAccessRepository);
    testEnv.start();
    String workflowId =
        WorkflowClient.start(createResourceAccessWF::createResourceRequest, tenantResourceV1)
            .getWorkflowId();
    testEnv.sleep(Duration.ofDays(1));
    verify(resourceAccessRepository).save(any());
    createResourceAccessWF.updateStatus(
        ResourceStatusEnum.APPROVED.toString(), ZonedDateTime.now());
    testEnv.sleep(Duration.ofSeconds(5));
    verify(resourceAccessRepository).update(any());
    List<PendingActivityInfo> pendingActivitiesList =
        testEnv
            .getWorkflowClient()
            .getWorkflowServiceStubs()
            .blockingStub()
            .describeWorkflowExecution(
                DescribeWorkflowExecutionRequest.newBuilder()
                    .setExecution(WorkflowExecution.newBuilder().setWorkflowId(workflowId).build())
                    .setNamespace(testEnv.getNamespace())
                    .build())
            .getPendingActivitiesList();
    assertEquals(0, pendingActivitiesList.size());
  }

When I use

@RegisterExtension
  public TestWorkflowExtension testWorkflowRule =
      TestWorkflowExtension.newBuilder()
          .registerWorkflowImplementationTypes(CreateResourceAccessWFImpl.class)
          .setDoNotStart(true)
          .build();

and call the UpdateWorkflow with ExternalWorkflowStub, I get a NOT FOUND error, which is expected. Now how do I mock aWorkflow which is already running on the server. Or is this not possible as part of Unit Test? Do I need to cover this in IT?

Hi,

You can use registerWorkflowImplementationFactory to mock workflows, see temporal_samples-java/core/src/test/java/io/temporal/samples/asyncuntypedchild/AsyncUntypedChildTest.java at 92350ae86e0727c68ce726b701124c621bf12941 · antmendoza/temporal_samples-java · GitHub .

Not sure if this answer yous question, if not , please can you provide a repro for what you are trying to do

Thanks.

Ah! Thank you so much. This will do.