Mocking child workflows in Java

Hello,

I am trying to figure out how to test some complex workflows.
I have a MainWorkflow that drives an execution and trigger several child workflows.
I would like to unit test MainWorkflow logic and do not care about child workflows as I will be creating separate unit tests for them.
Activities mocking is simple as I am registering activity instances to the worker (and I can mock them with mockito) but with workflows I have to register classes.
I didn’t find any tool for such workflow mocking in the TestWorkflowEnvironment.
I will be grateful for the hints.
SDK version: 1.14.0

thanks
Lukasz

See testMockedChild sample.

1 Like

Thank you @maxim. Somehow I missed that example.

Hi, is there any example of testing mocked child workflow using JUnit 5?

Hello @Tymofii_Voitenko

Check this out temporal_samples-java/HelloChildJUnit5Test.java at test-child-wf-junit5 · antmendoza/temporal_samples-java · GitHub

I will try to get this merged into the GitHub - temporalio/samples-java: Temporal Java SDK samples repository

Hope it helps,
Antonio

1 Like

Thanks! worked for me

How do I set the task queue with the TestWorkflowExtension for junit 5? I follow the sample code to create a test for my workflow that calls a child workflow, but the test hangs because my workflow specifies a task queue name when starting the child workflow. I had to revert to setting up the TestWorkflowEnvironment manually.

You can create a worker from test env that polls on the child workflow task queue, for example:

@RegisterExtension
  public static final TestWorkflowExtension testWorkflowExtension =
      TestWorkflowExtension.newBuilder()
          .setWorkflowTypes(MyWorkflowImpl.class)
          .setDoNotStart(true)
          .build();
// ...

@Test
public void testTripBookingFails(
  TestWorkflowEnvironment testEnv, Worker worker, MyWorkflowImpl workflow) {
  // create child wf worker
  Worker childWorker = testEnv.newWorker("my-child-tq");
  // register child wf impl with the child wf worker
  childWorker.registerWorkflowImplementationTypes(new ChildWorkflowImpl.class);
  testEnv.start();
// ...