Hello, I previously shared a blog post that explains how we use MockStatic in our workflow integration tests (see Mock Static Methods in Temporal Workflow Tests). Here’s a quick summary of our approach and the challenge we’re facing:
Our Approach Recap
In our current setup, we use a ContextPropagator because the default behavior of MockedStatic is thread-local. Since workflow execution spans multiple threads, MockStatic wouldn’t work as expected without propagating the stubbing behavior. We use this approach to write isolated tests for specific workflow blocks.
The Challenge
Our workflow is structured like this:
public class MyWorkflow {
void main() {
Block1.execute();
activity1.execute();
activity2.execute();
// rest of logic
}
}
public class Block1 {
static void execute() {
activityA.execute();
activityB.execute();
}
}
In our integration tests, we don’t want to re-test the logic inside Block1 every time. Instead, we need to mock Block1 (or its behavior) in our WorkflowIntegrationTest (hence the need for MockStatic). We do not want to put Block1 inside a ChildWorkflow since our code has too many blocks.
Having said that:
-
Is there a way to mock statically inside Temporal workflows? If yes, can you please assist; if not, what do you advise us to do (knowing that we do not want to go with sub-child workflows)?
-
We thought maybe we can force the Temporal SDK to work on a single thread in our test. Hence, can we provide Temporal with a pool of threads? If yes, how? Do you advise going with this approach?