You can inject the ZeebeClient and ZeebeTestEngine into your tests and listen to them.
With the ZeebeClient you can search for the current running job by the jobType you defined. The jobType should be the ServiceTask ID you defined in your BPMN
Example: completeServiceTasks Example
The problem is multiple jobs are the same type. For example, I have two service task:
type = "io.camunda:http-json:1"
element id = "ServiceTask_CreateBrazeProfile"
element instance key = 2251799813685258
and
type = "io.camunda:http-json:1"
element id = "ServiceTask_CreateSalesforceProfile"
element instance key = 2251799813685249
We feel these two service tasks are the same type: sending a REST request to an external API endpoint. The only differences are “REST API endpoint” and request body. Therefore, they share the same type.
final var activateJobsResponse =
zeebe.newActivateJobsCommand()
.jobType(jobType)
.maxJobsToActivate(count)
.send()
.join();
for (int i = 0; i < count; i++) {
final var job = activateJobsResponse.getJobs().get(i);
if (job.getElementId().equals("ServiceTask_CreateBrazeProfile")) {
zeebe.newCompleteCommand(job.getKey())
.variables(Collections.singletonMap("foo", "bar"))
.send()
.join();
} else {
zeebe.newCompleteCommand(job.getKey()).send().join();
}
}
This is still the completeServiceTasks Example but I adapt a little bit of the code.
It is going to loop over the current jobs and based on the elementId it would execute different complete commands.
I also figured out this solution, but it brings in a new issue. If there are multiple jobs are waiting to be either completed or failed, how could I complete one of them and put back all the others?
For example, my BPMN workflow uses a Parallel gateway to start 3 jobs at waiting stage. The zeebe.newActivateJobsCommand().jobType(jobType).maxJobsToActivate(count) activates those three jobs.
I just want to complete one job by its element id, and put the other two jobs back to the engine. I do not want to complete or fail the other two jobs.
Then I will assert the process instance.
Then I want to run the zeebe.newActivateJobsCommand().jobType(jobType).maxJobsToActivate(count) again to active the remaining two jobs. I will complete one of the two jobs, and put the last one back to engine.
But there is no command to put back an active job. As a result, the second execution of zeebe.newActivateJobsCommand().jobType(jobType) will active zero jobs.