Unit testing BPMN errors

Hello,
I’ve a simple diagram with a service task and a bpmn error attached to it.

The service task “Deliver goods” has the “Delegate expression” configuration and delegate expression is a bean implementing java delegate.
The delegate itself is calling a service “deliveryService” that I mock in the test.
I want to test that when a BPMN error is thrown by the service then the Manage error task is activated.
But unfortunately the error is not raised and Register DB activity is triggered instead.
Is my approach correct? What am I doing wrong?
That’s my code:

public class DeliveryUnitTest {

  @Mock
  DeliverService deliverService;

  @Deployment(resources = {"deliverybpmn"})
  @Test
  public void testDeliveryError() throws InterruptedException {


    when(deliverService.deliver(anyInt()))
        .thenThrow(new BpmnError("deliverError", "Error with delivering goods"));

    Map<String, Object> variables = new HashMap<String, Object>();
    variables.put("goods", "pc");
    variables.put("quantity", 3);

    ProcessInstance processInstance =
        runtimeService().startProcessInstanceByKey("DeliveryProcess", variables);


    assertThat(processInstance).isWaitingAt(findId("Manage error"));
    complete(task());
    assertThat(processInstance).isEnded();
  }
}

Thanks!

Hi Anna,

first of all, how is the service task implementation type are you using? I assume you are using expression.

There are three options: Java Class is very difficult to test, because you can’t mock it. Expression stores a symbolic string in the model like ${deliverService.deliver(execution.getAmount())}. If you want to mock it in test, you must tell your UnitTest (there is no SpringBoot IoC or Injection activated) how to resolve it.

There is a helper you should use:

Mocks.register("deliverService.deliver(execution.getAmount())", deliverService);

My approach is to use the third option: Expression Delegate. Then you have to implement the JavaDelegate and put your glue code to bind the process variables to your service invocation.
The advantage of this approach is that you can mock it very easily, and the good news is - there is a special library helping you to mock it: camunda-bpm-mockito.

Check it out: https://github.com/camunda-community-hub/camunda-bpm-mockito

Hope this helps.

Cheers,

Simon