Is there a way to start a unit test from the middle of the flow?

Hello community, I’m building some unit tests where my flow is something like A-B-C-D-E , but I would like to know if there’s a way to starting testing from C-D-E instead of A-B-C…

Thanks in advance

1 Like

@devBunny, you can try something like below:

public void startAtSpecificActivity() {
    ProcessInstance processInstance = runtimeService.createProcessInstanceQuery()
                                        .singleResult();

    runtimeService.createModification(processInstance.getId())
                               .startBeforeActivity("someServiceTask").execute();
}

To start after parallel gateway:

ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().singleResult();
runtimeService.createProcessInstanceModification(processInstance.getId())  
  .startBeforeActivity("assessCreditWorthiness")
  .startBeforeActivity("registerApplication")
  .execute();
1 Like

Hey @devBunny,

maybe you want to have a look at our Camunda BPM Assert examples we showcased at CamundaCon 2019, it is specifically addressing those issues.

Feel free to watch the accompanying video as well:

Basically, what @aravindhrs proposes works fine. However, if you want to start your instance directly at a specific point instead of regularly starting it and then modifying it afterwards, you can use something like

  @Test
  public void shouldDoWhateverYouSayItShould() {
    // given
    ProcessInstance instance = runtimeService()
        .createProcessInstanceByKey(PROCESS_KEY)
        .startBeforeActivity("C")
        .execute();
    // when
    ...
    // then
    assertThat(instance)
      .isActive()
      .isWaitingAt("D")
      ....;
  }

Hope that helps.

Best,
Tobias

3 Likes

Thank you.

Thank you!