Maven Central Build Status codecov Project Stats Apache License V.2

Table of Contents

simplify process mocking and testing

camunda-bpm-mockito is a community extension for the Camunda BPM process engine that aims to simplify and automate mocking of process applications.


Get started

Just include camunda-bpm-mockito in the test scope of your project:


gradle (kts):


Mocking of queries

Sometimes you want to test a Bean that uses the query API. Since the API is fluent, you would have to mock every single parameter call and let your service return the mocked query.

With the QueryMocks extension, you can do all this in just one line of code, see

  public class QueryMocksExample {
    private final TaskService taskService = mock(TaskService.class);
    private final Task task = mock(Task.class);

    public void mock_taskQuery() {
        // bind query-mock to service-mock and set result to task.
        final TaskQuery taskQuery = QueryMocks.mockTaskQuery(taskService).singleResult(task);

        final Task result = taskService.createTaskQuery().active().activityInstanceIdIn("foo").excludeSubtasks().singleResult();



Mock Listener and Delegate behavior

Mocking void methods using mockito is not very convenient, since you need to use the doAnswer(Answer<T>).when() construct, implement your own answer and pick up the parameter from the invocation context. JavaDelegate and ExecutionListener are providing their basic fuctionality using void methods.
In general, when working with the Delegate and Listener interfaces, there are basically two things they can do from the point of interaction between the process execution: modify process variables and raise errors.

We can use this to test bpmn-processes without relying on the delegate implementation.

public class FluentJavaDelegateMockTest {

  private static final String BEAN_NAME = "foo";
  private static final String MESSAGE = "message";

  public final ExpectedException thrown = ExpectedException.none();

  public void shouldThrowBpmnError() throws Exception {

    // expect exception

    DelegateExpressions.registerJavaDelegateMock(BEAN_NAME).onExecutionThrowBpmnError("code", MESSAGE);

    final JavaDelegate registeredDelegate = DelegateExpressions.getJavaDelegateMock(BEAN_NAME);

    // test succeeds when exception is thrown

Easy register and verify mocks

In addition two of the well-known "Mocks.register()" hook, you now have the possibility to register fluent mocks directly:


In the latter case, "YourDelegate" has to be annotated with @Named, @Component or @Service, depending on the injection framework you are using.

To verify the Mock execution, you can use


Auto mock all delegates and listeners

With the autoMock() feature, you can register all Delegates and Listeners at once, without explicitly adding "register"-statements to your testcase. If you do need to specify behaviour for the mocks, you can still get the mock via "getRegisteredJavaDelegateMock" (for delegates).

@Deployment(resources = "MockProcess.bpmn")
  public void register_mocks_for_all_listeners_and_delegates() throws Exception {

    final ProcessInstance processInstance = processEngineRule.getRuntimeService().startProcessInstanceByKey("process_mock_dummy");




Unit-testing listeners and JavaDelegates can be difficult, because the methods are void and only white-box testing via verify is possible. But most of the times, you just want to confirm that a certain variable was set (or a dueDate, a candidate, ...).

In these cases, use the Delegate fakes. The implement the interfaces DelegateTask and DelegateExecution, but are implemented as plain, fluent-styled Pojos.

So to test if your TaskListener

TaskListener taskListener = task -> {
  if (EVENTNAME_CREATE.equals(task.getEventName()) && "the_task".equals(task.getTaskDefinitionKey())) {
    task.addCandidateGroup((String) task.getVariableLocal("nextGroup"));

actually adds a candidateGroup that is read from a taskLocal variable on create, you can write a Test like this one:

public void taskListenerSetsCandidateGroup() throws Exception {
  // given a delegateTask 
  DelegateTask delegateTask = delegateTaskFake()
    .withVariableLocal("nextGroup", "foo");

  // when

  // then the candidate group was set


Mocking of external subprocesses

With ProcessExpressions.registerCallActivityMock() you can easily register a mocked process which is able to act with the following behaviours:

All of those methods could be combined on the fluent sub process mock builder.

The following example will e.g. register a process mock which does the following:

1) Wait until the given message SomeMessage gets correlated to the mock 2) Then wait until the given date waitUntilDate is reached 3) After this, a process variable foo is set with a value of `bar

      .onExecutionSetVariables(createVariables().putValue("foo", "bar"))

More examples could be found in the following class SubprocessMockExample.

Mocking of message correlation builder

Sometimes you have services or delegates responsible for the execution of message correlation with your process engine. Camunda provides a fluent builder API for creation a message correlation and running it.

class MyCorrelator {

  private final RuntimeService runtimeService;
  private final String value;
  private final String businessKey;

  MyCorrelator(RuntimeService runtimeService, String businessKey, String value) {
    this.runtimeService = runtimeService;
    this.value = value;
    this.businessKey = businessKey;

  void correlate() {
      .setVariable("myVar1", value)

In order to test those, you can use the following helper:

package org.camunda.bpm.extension.mockito;

import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.runtime.MessageCorrelationBuilder;
import org.junit.Test;

import static org.mockito.Mockito.*;

public class MessageCorrelationMockExample {

  public void mock_messageCorrelation() {

    // setup mock
    final RuntimeService runtimeService = mock(RuntimeService.class);
    final MessageCorrelationBuilder correlation = ProcessExpressions.mockMessageCorrelation(runtimeService, "MESSAGE_NAME");
    final MyCorrelator serviceUnderTest = new MyCorrelator(runtimeService, "my-business-key", "value-1");

    // execute correlation, e.g. in a class under test (service, delegate, whatever)

    // verify
    verify(correlation).setVariable("myVar1", "value-1");



Release Notes