Search code examples
bpmncamunda

How to test Event Listeners in Camunda?


I have used Execution and Task Listener in my process. How to unit test them using Junit in Camunda.


Solution

  • You could use for example the Camunda Model API and write a unit test to test your Execution listener.

    The unit test could look like the following:

      @Test
      public void testEndExecutionListenerIsCalledOnlyOnce() {
    
        BpmnModelInstance modelInstance = Bpmn.createExecutableProcess("process")
          .startEvent()
          .userTask()
          .camundaExecutionListenerClass(ExecutionListener.EVENTNAME_END, TestingExecutionListener.class.getName())
          .endEvent()
          .done();
    
        testHelper.deploy(modelInstance);
    
        // given
        ProcessInstance procInst = runtimeService.startProcessInstanceByKey("process");
        TaskQuery taskQuery = taskService.createTaskQuery().processInstanceId(procInst.getId());
    
        //when task is completed
        taskService.complete(taskQuery.singleResult().getId());
    
        // then end listener is called
        // assert something for example a variable is set or something else
      }
    

    For more examples see how Camunda tests the Execution Listeners in ExecutionListenerTest.java.