Unit testing a boundary timer attached to a connector

Hello,

I’m trying to test a bpmn flow where I have a connector which has an interrupt boundary timer attached to it. The timer then goes to a bpmn error exception like so:

image

To test the exception scenario what I am trying to do, is defining the timer duration with PT1S and then when the test runs I am hanging the connector execution by 10 seconds. The timer however is not being triggered.

Another approach that i tried was to execute the timer job when the process reaches the connector task. But once I use the ‘RuntimeService’ to instantiate a new process:

ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(bpmnKey, INPUT_VARIABLES);

The process executes until the end and I cannot acquire the timer job.

Do you know how can I implement a unit test to test the exception scenario? Any clue would be great.

Thank you

Hi @R1c2rdo,

attached interrupting bounday events on service task get only executed if you implement the service task as external.

A possible Junit test with the help of camunda-bpm-assert may look like this:

package org.camunda.bpm.unittest;

import static org.camunda.bpm.engine.test.assertions.ProcessEngineTests.*;
import static org.assertj.core.api.Assertions.*;

import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.camunda.bpm.engine.test.Deployment;
import org.camunda.bpm.engine.test.ProcessEngineRule;
import org.junit.Rule;
import org.junit.Test;

public class ServiceWithTimerTest {
  @Rule
  public ProcessEngineRule rule = new ProcessEngineRule();

  @Test
  @Deployment(resources = {"service_with_timer.bpmn"})
  public void shouldExecuteProcess() {
    ProcessInstance processInstance = runtimeService().startProcessInstanceByKey("serviceWithTimer");
    assertThat(processInstance).isWaitingAt(findId("SSH Execution")).externalTask().hasTopicName("ssh");
    execute(job(findId("10 seconds")));
    assertThat(processInstance).isEnded().hasPassed(findId("End with Exception"));
  }
}

Hope this helps, Ingo

2 Likes

Hi Ingo,

Thank you very much for your response.

for this flow I must use the service task as connector instead of external as the logic is implemented in a java class. is there other way I could validate if the connector takes too long and redirect the flow to the exception?

Thank you

Hi @R1c2rdo

May you explain why?

No, it isn’t. You can just add transaction boundaries before or after a service task implemented with connectors. But the attached boundary events needs a transaction boundary after creating the service task.

Hope this helps, Ingo

You could also do the time management in java. If you detect that the call lasts for too long, you have all the options open as to how to signal it. But be aware that you’ll have to work with two threads which might bring camunda transaction management off the path. You have to check what happens then.

1 Like