Issue while unit testing code to retrieve status of Workflow

I have a function in my code to retrieve the status of a Workflow using DescribeWorkflowExecutionRequest. I’m getting the Workflow status, list of pending activities, and also some history events:

public Status getStatus(String namespace, String instanceName) {
   WorkflowServiceStubs workflowServiceStubs = workflowClient.getWorkflowServiceStubs();
   DescribeWorkflowExecutionRequest request =
          DescribeWorkflowExecutionRequest.newBuilder()
              .setExecution(WorkflowExecution.newBuilder().setWorkflowId(temporalWorkflowId))
              .setNamespace(TEMPORAL_NAMESPACE)
              .build();

   DescribeWorkflowExecutionResponse response =
          workflowServiceStubs.blockingStub().describeWorkflowExecution(request);
  
   String workflowStatus = String.valueOf(response.getWorkflowExecutionInfo().getStatus());
   List<PendingActivityInfo> pendingActivityInfoList = response.getPendingActivitiesList();
   // Fetch Workflow Execution history events
   String temporalRunId = response.getWorkflowExecutionInfo().getExecution().getRunId();
   WorkflowExecutionHistory workflowExecutionHistory =
          workflowClient.fetchHistory(temporalWorkflowId, temporalRunId);
}

I need to unit test this code, and I thought I could call this function from a unit test in which I’m starting a Workflow (and it finishes), providing the same namespace and Workflow ID. I think it might be a problem with the workflowClient bean that my code above is using…it’s probably not the same bean that’s being created in the test. Is there a way I can inject the workflowClient object? Or is there another way I can do this?

@RegisterExtension
  public final TestWorkflowExtension testWorkflowExtension =
      TestWorkflowExtension.newBuilder()
          .setNamespace("jenkins-upgrade-process-ns")
          .setWorkflowTypes(UpgradeInstanceWorkflowImpl.class)
          .setDoNotStart(true)
          .build();

@Test
public void testBeginUpgradeInstance(TestWorkflowEnvironment testWorkflowEnvironment,
      Worker worker,
      UpgradeInstanceWorkflow workflow) {
  // Mock activities
  UpgradeInstanceActivity activities =
        mock(activity.getClass(), withSettings().withoutAnnotations());
  doNothing().when(activities).backupJenkinsMasterActivity(any());
  worker.registerActivitiesImplementations(activities);
  testWorkflowEnvironment.start();
  String result = workflow.beginUpgradeInstance(upgradeRequest);
  assertEquals("Upgrade Successful", result);

  // Now I call my function that returns the status
  upgradeProcessService.getStatus("Test-Namespace", "Test-Instance");
}

I’m seeing this error:

io.grpc.StatusRuntimeException: NOT_FOUND: Execution not found in mutable state: WorkflowId{namespace='jenkins-upgrade-process-ns', workflowId='UpgradeInstanceWorkflowTest'}

From my test I can actually see the execution history of the Workflow that started:

Workflow execution histories:
Stored Workflows:
ExecutionId{namespace='jenkins-upgrade-process-ns', execution=workflow_id: "8472c058-f755-4610-8637-b8c3bbc7b6cf"
run_id: "b58f8278-7948-4736-acd6-bace9ea58b2c"
}

event_id: 1
event_time { seconds: 1695918288 nanos: 755000000 }
event_type: EVENT_TYPE_WORKFLOW_EXECUTION_STARTED
workflow_execution_started_event_attributes { 
  workflow_type { name: "UpgradeInstanceWorkflow" }
  task_queue { name: "WorkflowTest-testBeginUpgradeInstance(TestWorkflowEnvironment, Worker, UpgradeInstanceWorkflow)-[engine:junit-jupiter]/[class:com.nvidia.ipp.jenkinsupgradeprocess.temporal.UpgradeInstanceWorkflowTest]/[method:testBeginUpgradeInstance(io.temporal.testing.TestWorkflowEnvironment, io.temporal.worker.Worker, com.nvidia.ipp.jenkinsupgradeprocess.temporal.UpgradeInstanceWorkflow)]" }
  input {
    payloads {
...

Appreciate any advice, thanks!

Yes most likely it’s not same WorkflowClient, you could pass WorkflowClient as input to your getStatus method and from test use the TestWorkflowEnvironment client, something like:

upgradeProcessService.getStatus(“Test-Namespace”, “Test-Instance”, testWorkflowEnvironment.getWorkflowClient());

or if you are using SpringBoot, consider using the Temporal SDK SpringBoot extension

Passing the WorkflowClient object was a good idea. Although it didn’t work for my test, I modified my code using this idea. Thanks Tihomir!