# Issues with Unit Testing a Workflow with signal method

**URL:** <https://community.temporal.io/t/issues-with-unit-testing-a-workflow-with-signal-method/1735>\
**Category:** Community Support\
**Tags:** java-sdk\
**Created:** [March 16, 2021, 10:36am UTC](https://community.temporal.io/t/issues-with-unit-testing-a-workflow-with-signal-method/1735 "2021-03-16T10:36:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anjo](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/anjo/32/806_2.png) [@anjo](https://community.temporal.io/u/anjo)\
**Post date:** [March 16, 2021, 10:36am UTC](https://community.temporal.io/t/issues-with-unit-testing-a-workflow-with-signal-method/1735/1 "2021-03-16T10:36:15Z")

</div>

Hello  
I am using java temporal-sdk v1.0.6.  
I am trying to test a Workflow with signal method using mocks for Activity.  
Sample code is here [Comparing temporalio:master...ansujohn:SignalWF\_Test · temporalio/samples-java · GitHub](https://github.com/temporalio/samples-java/compare/master...ansujohn:SignalWF_Test?expand=1)

Couple of issues that i get in [samples-java/HelloSignalWorkflowTest.java at SignalWF\_Test · ansujohn/samples-java · GitHub](https://github.com/ansujohn/samples-java/blob/SignalWF_Test/src/test/java/io/temporal/samples/helloSignal/HelloSignalWorkflowTest.java#L19)

1. Test failing with error “ **Actually, there were zero interactions with this mock** ”
2. Though signalChange method is called twice, processing is done in the Workflow for the first call only [samples-java/HelloSignalWorkflowTest.java at SignalWF\_Test · ansujohn/samples-java · GitHub](https://github.com/ansujohn/samples-java/blob/SignalWF_Test/src/test/java/io/temporal/samples/helloSignal/HelloSignalWorkflowTest.java#L73)
3. Without a sleep in the test - [samples-java/HelloSignalWorkflowTest.java at SignalWF\_Test · ansujohn/samples-java · GitHub](https://github.com/ansujohn/samples-java/blob/SignalWF_Test/src/test/java/io/temporal/samples/helloSignal/HelloSignalWorkflowTest.java#L71), tasks simply run and complete without any processing, doesn’t wait for signals, no debug outputs. Why is the sleep required, what is the recommended value, wouldn’t this increase the test execution time?

Am i missing some configuration?  
Appreciate the help.  
Thank you

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [March 16, 2021, 7:12pm UTC](https://community.temporal.io/t/issues-with-unit-testing-a-workflow-with-signal-method/1735/3 "2021-03-16T19:12:05Z")

</div>

The following test passes:

```java
  @Test
  public void testSignal() {
    GreetingActivity greetingActivity = mock(GreetingActivity.class);
    when(greetingActivity.composeGreeting(anyString())).thenReturn("Hello Test");

    worker.registerActivitiesImplementations(greetingActivity);
    testEnv.start();

    // Get a workflow stub using the same task queue the worker uses.
    WorkflowOptions workflowOptions =
        WorkflowOptions.newBuilder()
            .setTaskQueue(TEST_QUEUE)
            .setWorkflowTaskTimeout(Duration.ofSeconds(20))
            .build();
    HelloSignalWorkflow workflow =
        client.newWorkflowStub(HelloSignalWorkflow.class, workflowOptions);

    // Start workflow
    WorkflowClient.start(workflow::start);

    workflow.signalChange("INIT");
    workflow.signalChange("STATE1");

    testEnv.sleep(Duration.ofSeconds(2));
    verify(greetingActivity, times(2)).composeGreeting(any());
  }
}

```

Note that signals are asynchronous. The `workflow.signalChange` doesn’t wait for the workflow to process a signal. It returns immediately as soon as Temporal service queues up the signal for delivery. So your test sends two signals asynchronously and immediately checks for results without giving the workflow any chance to process them.

There is an issue that I need to troubleshoot. `testEnv.sleep(Duration.ofSeconds(2))` doesn’t skip time in this scenario as it should. I’m looking into this.

---

<div class="post-metadata">

**Author:** ![anjo](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/anjo/32/806_2.png) [@anjo](https://community.temporal.io/u/anjo)\
**Post date:** [March 17, 2021, 10:19am UTC](https://community.temporal.io/t/issues-with-unit-testing-a-workflow-with-signal-method/1735/4 "2021-03-17T10:19:27Z")

</div>

> [@maxim](#):
>
> `testEnv.sleep(Duration.ofSeconds(2));`

Thank you for the response.

So introducing testEnv.sleep(Duration.ofSeconds(2)); after/between signalChange calls allow the workflow to process them, is that right?

Will adding WorkflowStub.fromTyped(workflow).getResult(Void.class); before assertions help?

Yes there is an issue with testEnv.sleep as it doesn’t skip time and hence increases test run time.

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [March 17, 2021, 3:49pm UTC](https://community.temporal.io/t/issues-with-unit-testing-a-workflow-with-signal-method/1735/5 "2021-03-17T15:49:05Z")

</div>

Yes, I think adding `getResult` to wait for the workflow completion would help assuming that the workflow completes.
