cancel
Showing results for 
Search instead for 
Did you mean: 

problem unit testing with async=true

gertp
Champ in-the-making
Champ in-the-making
Hello, when running a simple test case and setting a service task to async it does not execute my Service class
This is the workflow:

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test">
  <process id="myProcess" name="My process" isExecutable="true">
    <startEvent id="startevent1" name="Start"></startEvent>
    <endEvent id="endevent1" name="End"></endEvent>
    <serviceTask id="servicetask1" name="Some Service" activiti:async="true" activiti:class="RunService"></serviceTask>
    <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="servicetask1"></sequenceFlow>
    <sequenceFlow id="flow2" sourceRef="servicetask1" targetRef="endevent1"></sequenceFlow>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_myProcess">
    <bpmndi:BPMNPlane bpmnElement="myProcess" id="BPMNPlane_myProcess">
      <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="290.0" y="130.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="600.0" y="130.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="servicetask1" id="BPMNShape_servicetask1">
        <omgdc:Bounds height="55.0" width="105.0" x="420.0" y="120.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
        <omgdi:waypoint x="325.0" y="147.0"></omgdi:waypoint>
        <omgdi:waypoint x="420.0" y="147.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
        <omgdi:waypoint x="525.0" y="147.0"></omgdi:waypoint>
        <omgdi:waypoint x="600.0" y="147.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>

This is the service class:


import org.activiti.engine.impl.pvm.delegate.ActivityBehavior;
import org.activiti.engine.impl.pvm.delegate.ActivityExecution;


public class RunService implements ActivityBehavior {

   public void execute(ActivityExecution arg0) throws Exception {
      System.out.println("executing RunService");
   }
}


and this is the unit test:

package test.java.org.activiti.designer.test;

import static org.junit.Assert.assertNotNull;

import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Map;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.test.ActivitiRule;
import org.junit.Rule;
import org.junit.Test;

public class ProcessTestMyProcess {

   private String filename = "/resources/TestAsync.bpmn";

   @Rule
   public ActivitiRule activitiRule = new ActivitiRule();

   @Test
   public void startProcess() throws Exception {
      RepositoryService repositoryService = activitiRule.getRepositoryService();
      repositoryService.createDeployment().addInputStream("myProcess.bpmn20.xml",
            new FileInputStream(filename)).deploy();
      RuntimeService runtimeService = activitiRule.getRuntimeService();
      Map<String, Object> variableMap = new HashMap<String, Object>();
      variableMap.put("name", "Activiti");
      ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("myProcess", variableMap);
      assertNotNull(processInstance.getId());
      System.out.println("id " + processInstance.getId() + " "
            + processInstance.getProcessDefinitionId());
   }
}



When setting  to activiti:async="true", the service class is never called (no println and no debugger breakpoint work here), setting it to false does make it get called. Am I misunderstanding how async should work here and is there a way for a unit test to work with async = true ?
Thank you.
1 REPLY 1

jbarrez
Star Contributor
Star Contributor
Probably your job executor is not enabled: your process engine config needs to enable that.

Also: in your unit test you will need to wait for the job executor to pick it up (it polls the db every xx seconds). See other unit tests in the activiti-engine module around that.