Unit testing Temporal Workflow using Spock and Groovy

Hi,
While trying to test Temporal workflow(using java temporal-sdk - version 1.28.3) using Spock and Groovy, we are facing the below exception.

java.lang.Error: Called from non workflow or workflow callback thread

at io.temporal.internal.sync.DeterministicRunnerImpl.currentThreadInternal(DeterministicRunnerImpl.java:130)
at io.temporal.internal.sync.CompletablePromiseImpl.<init>(CompletablePromiseImpl.java:65)
at io.temporal.internal.sync.WorkflowInternal.newCompletablePromise(WorkflowInternal.java:128)
at io.temporal.workflow.Workflow.newPromise(Workflow.java:533)
at io.temporal.internal.sync.AsyncInternal.execute(AsyncInternal.java:299)
at io.temporal.internal.sync.AsyncInternal.function(AsyncInternal.java:56)
at io.temporal.workflow.Async.function(Async.java:38)

How to resolve this?

Are you using TestWorkflowEnvironment to test it?

We aren’t using TestWorkflowEnvironment because we are just trying to write unit tests and for that we are just using Spock and Groovy.
We plan to use TestWorkflowEnvironment for integration tests though.
But we need to resolve this issue in unit tests for achieving the code coverage.

Also, we are using DynamicWorkflow implementation for the workflow.

You must use TestWorkflowEnvironment for unit testing workflows.