# Mocking child workflows in Java

**URL:** <https://community.temporal.io/t/mocking-child-workflows-in-java/5598>\
**Category:** Community Support\
**Tags:** java-sdk\
**Created:** [August 5, 2022, 8:39pm UTC](https://community.temporal.io/t/mocking-child-workflows-in-java/5598 "2022-08-05T20:39:27Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![lukasz.a](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/lukasz.a/32/2582_2.png) [@lukasz.a](https://community.temporal.io/u/lukasz.a)\
**Post date:** [August 5, 2022, 8:39pm UTC](https://community.temporal.io/t/mocking-child-workflows-in-java/5598/1 "2022-08-05T20:39:27Z")

</div>

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

---

<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:** [August 5, 2022, 11:24pm UTC](https://community.temporal.io/t/mocking-child-workflows-in-java/5598/2 "2022-08-05T23:24:32Z")

</div>

See [testMockedChild](https://github.com/temporalio/samples-java/blob/a551947db6d742f288c3961e2f41f2afd196757b/src/test/java/io/temporal/samples/hello/HelloChildTest.java#L65) sample.

---

<div class="post-metadata">

**Author:** ![lukasz.a](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/lukasz.a/32/2582_2.png) [@lukasz.a](https://community.temporal.io/u/lukasz.a)\
**Post date:** [August 5, 2022, 11:42pm UTC](https://community.temporal.io/t/mocking-child-workflows-in-java/5598/3 "2022-08-05T23:42:00Z")

</div>

Thank you @maxim. Somehow I missed that example.

---

<div class="post-metadata">

**Author:** ![Tymofii\_Voitenko](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/tymofii_voitenko/32/2727_2.png) [@Tymofii\_Voitenko](https://community.temporal.io/u/Tymofii_Voitenko)\
**Post date:** [September 6, 2022, 8:44am UTC](https://community.temporal.io/t/mocking-child-workflows-in-java/5598/4 "2022-09-06T08:44:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![antonio.perez](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/antonio.perez/32/2417_2.png) [@antonio.perez](https://community.temporal.io/u/antonio.perez)\
**Post date:** [September 6, 2022, 11:00am UTC](https://community.temporal.io/t/mocking-child-workflows-in-java/5598/5 "2022-09-06T11:00:29Z")

</div>

Hello @Tymofii_Voitenko

Check this out [temporal\_samples-java/HelloChildJUnit5Test.java at test-child-wf-junit5 · antmendoza/temporal\_samples-java · GitHub](https://github.com/antmendoza/temporal_samples-java/blob/test-child-wf-junit5/src/test/java/io/temporal/samples/hello/HelloChildJUnit5Test.java)

I will try to get this merged into the [GitHub - temporalio/samples-java: Temporal Java SDK samples](https://github.com/temporalio/samples-java) repository

Hope it helps,  
Antonio

---

<div class="post-metadata">

**Author:** ![Tymofii\_Voitenko](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/tymofii_voitenko/32/2727_2.png) [@Tymofii\_Voitenko](https://community.temporal.io/u/Tymofii_Voitenko)\
**Post date:** [September 6, 2022, 12:39pm UTC](https://community.temporal.io/t/mocking-child-workflows-in-java/5598/6 "2022-09-06T12:39:14Z")

</div>

Thanks! worked for me

---

<div class="post-metadata">

**Author:** ![davidc](https://avatars.discourse-cdn.com/v4/letter/d/a183cd/32.png) [@davidc](https://community.temporal.io/u/davidc)\
**Post date:** [February 22, 2023, 10:58am UTC](https://community.temporal.io/t/mocking-child-workflows-in-java/5598/8 "2023-02-22T10:58:50Z")

</div>

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.

---

<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:** [February 23, 2023, 3:26pm UTC](https://community.temporal.io/t/mocking-child-workflows-in-java/5598/9 "2023-02-23T15:26:22Z")

</div>

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

```auto
@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();
// ...

```
