Environment : Java 17 Spring Boot, Junit5
Error in console:
java.lang.AssertionError: Expected element with id task1 to be passed 1 times, but was 0
at io.camunda.zeebe.process.test.assertions.ProcessInstanceAssert.hasPassedElement(ProcessInstanceAssert.java:225)
at io.camunda.zeebe.process.test.assertions.ProcessInstanceAssert.hasPassedElement(ProcessInstanceAssert.java:188)
at tv.cadent.workflow.example.Task1ServiceTest.task1ServiceCompleteTest(Task1ServiceTest.java:124)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:725)
at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84)
at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$7(TestMethodTestDescriptor.java:214)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:210)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:135)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:66)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:151)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:35)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:54)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:107)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:88)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:54)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:67)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:52)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:114)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:95)
at org.junit.platform.launcher.core.DefaultLauncherSession$DelegatingLauncher.execute(DefaultLauncherSession.java:91)
at org.junit.platform.launcher.core.SessionPerRequestLauncher.execute(SessionPerRequestLauncher.java:60)
at org.eclipse.jdt.internal.junit5.runner.JUnit5TestReference.run(JUnit5TestReference.java:98)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:40)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:529)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:756)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:452)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)
Test Class
@WorkflowServiceZeebeSpringTest
@TestMethodOrder(OrderAnnotation.class)
@TestInstance(Lifecycle.PER_CLASS)
@ExtendWith(SpringExtension.class)
public class Task1ServiceTest {
private static final Logger log = LoggerFactory.getLogger(Task1Service.class);
@MockBean private ZeebeTestEngineProxy zeebeTestEngineProxy;
@MockBean private ZeebeClientProxy zeebeClientProxy;
@Autowired private ZeebeTestEngine zeebeTestEngine;
@InjectMocks private Task1Service task1Service;
private Map<String, Object> variables;
private ZeebeClient zeebeClient;
private static final String PROCESS_ID = "generic-workflow-task1-test-process";
private static final String TASK1_ELEMENT_ID = "task1";
private static final String TASK1_SERVICE_JOB_TYPE = "task1-service-worker";
@BeforeAll
public void beforeAllInitialize() {
zeebeClient = zeebeTestEngine.createClient();
zeebeClient.newDeployResourceCommand().addResourceFromClasspath("bpmn/example/task1-test.bpmn").send().join();
}
@BeforeEach
public void beforeEachInitialize() {
}
@AfterAll
public void afterAllClose() {
zeebeClient.close();
}
@Order(1)
@Test
public void task1ServiceFailTest() throws Exception {
try {
// Variables for testing failure of tast1 service
Map<String,Object> variables = new HashMap<>();
variables.put("name", "HappyProcess");
variables.put("taskToFail", "Task1");
// start a process instance
ProcessInstanceEvent processInstanceFail = startProcessInstance(variables);
assertThat(processInstanceFail).isActive();
// Activated Job
ActivateJobsResponse response = zeebeClient.newActivateJobsCommand().jobType(TASK1_SERVICE_JOB_TYPE)
.maxJobsToActivate(1).send().join();
ActivatedJob activatedJob = getActivatedJob(response);
JobAssert assertions = BpmnAssert.assertThat(activatedJob);
assertions.hasNoIncidents();
task1Service.task1Service(activatedJob, zeebeClient);
assertThat(processInstanceFail).hasNotPassedElement(TASK1_ELEMENT_ID);
assertThat(processInstanceFail).isNotCompleted();
cancelProcessInstance(processInstanceFail);
} catch (Exception e) {
log.error("Error in task1 service fail test " + e.getMessage());
}
}
@Order(2)
@Test
public void task1ServiceCompleteTest() throws Exception {
try {
// Variables for testing completion of tast1 service
Map<String,Object> variables = new HashMap<>();
variables.put("name", "HappyProcess");
variables.put("taskToFail", ",");
// start a process instance
ProcessInstanceEvent processInstance = startProcessInstance(variables);
assertThat(processInstance).isActive();
BpmnAssert.assertThat(processInstance);
ActivateJobsResponse response = zeebeClient.newActivateJobsCommand().jobType(TASK1_SERVICE_JOB_TYPE)
.maxJobsToActivate(1).send().join();
ActivatedJob activatedJob = getActivatedJob(response);
assertThat(processInstance).hasNotPassedElement(TASK1_ELEMENT_ID);
task1Service.task1Service(activatedJob, zeebeClient);
int duration = 10000;// wait till Task1 Service run
Thread.sleep(duration);
assertThat(processInstance).hasNoIncidents();
assertThat(processInstance).hasPassedElement(TASK1_ELEMENT_ID);
assertThat(processInstance).isCompleted();
cancelProcessInstance(processInstance);
} catch (Exception e) {
log.error("Error in task1 service comelete test " + e.getMessage());
}
}
private ActivatedJob getActivatedJob(ActivateJobsResponse response) throws Exception {
int duration = 1000;
while(response.getJobs().size()<1) {
Thread.sleep(duration);
duration+=1000;
if(duration == 10000)
throw new Exception("Job waiting period exceeded");
}
return response.getJobs().get(0);
}
private ProcessInstanceEvent startProcessInstance(Map<String, Object> variables) {
return zeebeClient.newCreateInstanceCommand().bpmnProcessId(PROCESS_ID).latestVersion().variables(variables)
.send().join();
}
private void cancelProcessInstance(ProcessInstanceEvent processInstance) {
zeebeClient.newCancelInstanceCommand(processInstance.getProcessInstanceKey()).send();
}
}
Service Task Class
@Component
public class Task1Service {
private Logger log = LoggerFactory.getLogger(this.getClass());
/*
* Simple Zeebe Client Asynchronous, short run and provide immediate response
* Here type is reference from BPMN model, type must be unique within BPMN model
* autoComplete helps to complete job without any intervention or code and the
* Spring integration will take care of job completion otherwise job handler
* code has to also complete the job.
*/
@ZeebeWorker(type = "task1-service-worker")
public void task1Service(final ActivatedJob job, final JobClient client) throws Exception {
try {
log.info("Task1 Service is running, job key: {}", job.getKey());
Map<String, Object> variableMap = job.getVariablesAsMap();
Object taskToFail = variableMap.get("taskToFail");
if (taskToFail != null) {
boolean isFail = taskToFail.toString().trim().equalsIgnoreCase("task1");
if (!isFail) {
int sleepDurationSecs = 10;
log.info("Simulating short run by sleeping for {} seconds...", sleepDurationSecs);
TimeUnit.SECONDS.sleep(sleepDurationSecs);
client.newCompleteCommand(job).send();
log.info("Task1 Service is completed");
} else {
client.newFailCommand(job).retries(0).send();
log.info("Task1 Service is failed");
}
} else {
throw new ExampleServiceException("taskToFail variable is not available");
}
} catch (InterruptedException ie) {
log.error("Error in Task1 Service. Sleeping is interrupted.");
throw ie;
} catch (ExampleServiceException ese) {
log.error("Error in Task1 Service.");
throw ese;
} catch (Exception e) {
log.error("Error in Task1 Service.");
throw new ExampleServiceException("Error in Task1 Service", e);
}
}
}
BPMN Model
<?xml version="1.0" encoding="UTF-8"?>
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:modeler="http://camunda.org/schema/modeler/1.0" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:zeebe="http://camunda.org/schema/zeebe/1.0" xmlns:camunda="http://camunda.org/schema/1.0/bpmn" id="Definitions_1" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Web Modeler" exporterVersion="174e755" modeler:executionPlatform="Camunda Cloud" modeler:executionPlatformVersion="8.0.0" camunda:diagramRelationId="b3624121-4554-4eb5-b30e-5010e463abb3">
<bpmn:process id="generic-workflow-task1-test-process" name="Task1 Service Test Process" isExecutable="true">
<bpmn:startEvent id="process-start-event" name="Process Start Event">
<bpmn:outgoing>Flow_1uhj8re</bpmn:outgoing>
</bpmn:startEvent>
<bpmn:sequenceFlow id="Flow_1uhj8re" sourceRef="process-start-event" targetRef="task1" />
<bpmn:endEvent id="process-end-event" name="Process End Event">
<bpmn:incoming>Flow_1rwiwoz</bpmn:incoming>
</bpmn:endEvent>
<bpmn:sequenceFlow id="Flow_1rwiwoz" sourceRef="task1" targetRef="process-end-event" />
<bpmn:serviceTask id="task1" name="Task1 Service">
<bpmn:extensionElements>
<zeebe:taskDefinition type="task1-service-worker" />
<zeebe:ioMapping>
<zeebe:input source="=name" target="name" />
<zeebe:input source="=taskToFail" target="taskToFail" />
</zeebe:ioMapping>
</bpmn:extensionElements>
<bpmn:incoming>Flow_1uhj8re</bpmn:incoming>
<bpmn:outgoing>Flow_1rwiwoz</bpmn:outgoing>
</bpmn:serviceTask>
</bpmn:process>
<bpmndi:BPMNDiagram id="BPMNDiagram_1">
<bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="generic-workflow-task1-test-process">
<bpmndi:BPMNEdge id="Flow_1rwiwoz_di" bpmnElement="Flow_1rwiwoz">
<di:waypoint x="340" y="118" />
<di:waypoint x="402" y="118" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="Flow_1uhj8re_di" bpmnElement="Flow_1uhj8re">
<di:waypoint x="186" y="118" />
<di:waypoint x="240" y="118" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNShape id="_BPMNShape_StartEvent_2" bpmnElement="process-start-event">
<dc:Bounds x="150" y="100" width="36" height="36" />
<bpmndi:BPMNLabel>
<dc:Bounds x="136" y="143" width="67" height="27" />
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Event_0xrz09m_di" bpmnElement="process-end-event">
<dc:Bounds x="402" y="100" width="36" height="36" />
<bpmndi:BPMNLabel>
<dc:Bounds x="389" y="143" width="63" height="27" />
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Activity_00h0zqo_di" bpmnElement="task1">
<dc:Bounds x="240" y="78" width="100" height="80" />
</bpmndi:BPMNShape>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</bpmn:definitions>
What’s the issue? how to resolve it? Is My test cases not right? Please provide complete in depth solution.