# Issue while unit testing code to retrieve status of Workflow

**URL:** <https://community.temporal.io/t/issue-while-unit-testing-code-to-retrieve-status-of-workflow/9645>\
**Category:** Community Support\
**Tags:** java-sdk, testing\
**Created:** [September 28, 2023, 4:41pm UTC](https://community.temporal.io/t/issue-while-unit-testing-code-to-retrieve-status-of-workflow/9645 "2023-09-28T16:41:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lss](https://avatars.discourse-cdn.com/v4/letter/l/278dde/32.png) [@lss](https://community.temporal.io/u/lss)\
**Post date:** [September 28, 2023, 4:41pm UTC](https://community.temporal.io/t/issue-while-unit-testing-code-to-retrieve-status-of-workflow/9645/1 "2023-09-28T16:41:02Z")

</div>

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:

```auto
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?

```auto
@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:

```auto
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:

```auto
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!

---

<div class="post-metadata">

**Author:** ![tihomir](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/tihomir/32/6580_2.png) [@tihomir](https://community.temporal.io/u/tihomir)\
**Post date:** [September 28, 2023, 4:47pm UTC](https://community.temporal.io/t/issue-while-unit-testing-code-to-retrieve-status-of-workflow/9645/2 "2023-09-28T16:47:02Z")

</div>

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](https://github.com/temporalio/sdk-java/tree/master/temporal-spring-boot-autoconfigure-alpha)

---

<div class="post-metadata">

**Author:** ![lss](https://avatars.discourse-cdn.com/v4/letter/l/278dde/32.png) [@lss](https://community.temporal.io/u/lss)\
**Post date:** [September 29, 2023, 12:03am UTC](https://community.temporal.io/t/issue-while-unit-testing-code-to-retrieve-status-of-workflow/9645/3 "2023-09-29T00:03:30Z")

</div>

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!
