How to test BPMN with execution listener in a Camunda Spring Boot application?

Viewed 1048

In my BPMN I set a variable in an execution listener as documented in Internal Context Variables:

The following example shows an expression which sets the variable test to the current event name of an execution listener.

<camunda:executionListener event="start"
 expression="${execution.setVariable('test', execution.eventName)}" />

I try to test this BPMN in a JUnit test of a Camunda Spring Boot application, but the execution listener is not applied.

BPMN:

  <bpmn:process id="test_bpmn" isExecutable="true">
    <bpmn:startEvent id="StartEvent_1">
      <bpmn:extensionElements>
        <camunda:executionListener expression="execution.setVariable(&#34;test&#34;, &#34;success&#34;)" event="start" />
      </bpmn:extensionElements>
      <bpmn:outgoing>SequenceFlow_0hgrhd2</bpmn:outgoing>
    </bpmn:startEvent>
    <bpmn:sequenceFlow id="SequenceFlow_0hgrhd2" sourceRef="StartEvent_1" targetRef="Task_11dxb5y" />
    <bpmn:userTask id="Task_11dxb5y">
      <bpmn:incoming>SequenceFlow_0hgrhd2</bpmn:incoming>
    </bpmn:userTask>
  </bpmn:process>

JUnit test:

@Deployment(resources = {"test.bpmn"})
public class MyTest {

    @Rule
    public ProcessEngineRule rule = new StandaloneInMemoryTestConfiguration().rule();

    @Test
    public void test() {

    ProcessInstance instance = rule.getRuntimeService().startProcessInstanceByKey("test_bpmn");
    assertThat(instance).isActive().hasVariables("test");
}

Error:

java.lang.AssertionError: Expecting actual ProcessInstance {id='4', processDefinitionId='test_bpmn:1:3', businessKey='null'} to hold process variables [test], instead we found it to hold no variables at all.
  at org.camunda.bpm.engine.test.assertions.ProcessInstanceAssert.hasVars(ProcessInstanceAssert.java:284)
  at org.camunda.bpm.engine.test.assertions.ProcessInstanceAssert.hasVariables(ProcessInstanceAssert.java:257)
  at com.mycompany.MyTest.test(MyTest.java:24)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
  at java.lang.reflect.Method.invoke(Unknown Source)
  at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
  at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
  at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
  at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
  at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:55)
  at org.camunda.bpm.engine.test.ProcessEngineRule$1.evaluate(ProcessEngineRule.java:165)
  at org.junit.rules.RunRules.evaluate(RunRules.java:20)
  at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
  at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
  at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
  at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
  at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
  at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
  at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
  at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
  at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
  at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
  at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)

What did I wrong?

0 Answers
Related