Hello,
I have modeled a simple BPMN process with two Tasks:
StartEvent --> TaskOne --> TaskTwo --> EndEvent
To test the BPMN model, I created the following UnitTest, which works fine as long as ‘asynchronous before’ is not checked at any of the service tasks.
@Deployment(resources = { "process.bpmn" })
public class ProcessTest {
@Rule
public ProcessEngineRule processEngineRule = new ProcessEngineRule();
@Mock
private Process process;
@Before
public void init() {
MockitoAnnotations.initMocks(this);
Mocks.register("process", process);
}
@Test
public void test() {
RuntimeService runtimeService = processEngineRule.getRuntimeService();
ProcessInstance processInstance = runtimeService.createProcessInstanceByKey("process").execute();
assertThat(processInstance).hasPassedInOrder(new String[] { "Activity_TaskOne", "Activity_TaskTwo" });
assertThat(processInstance).isEnded();
}
}
After checking ‘asynchronous before’ at both service tasks, the unit test outputs:
java.lang.AssertionError: Expecting org.camunda.bpm.engine.impl.persistence.entity.ProcessInstanceWithVariablesImpl@21c815e4
to have passed activities [Activity_TaskOne, Activity_TaskTwo] at least once and in order,
but actually we instead we found that it passed [StartEvent].
(Please make sure you have set the history service of the engine to at least 'activity' or a higher level before making use of this assertion!)
As far as I understand asychronous continuations this makes sense, but how can this be solved? Are there any other possibilities to unit test process flows with async service tasks or do I something wrong?
This would be a little bit unhandy for huge BPMN processes. Of course, I could write my own implementation of a ‘hasPassedInOrder’ and switch of the JobExecutor, but I expect that such an issue has been solved before and that there’s a simple solution also when the JobExecutor has been activated.
if you enable the jobExecutor for the test, you have to wait until the job is done. Maybe a Thread.sleep(1000) is sufficient, maybe not (it depends on your hardware).