Mock JobWorker with ZeebeProcessTest

Hello I am trying to create integration tests for BPMN files and want to test them with Mock Job workers using the embedded engine. Is this currently supported with Camunda 8/Zeebe? The integration tests work fine when using a Script Task, but fail when using Service Task that is associated with a JobWorker. It tries to connect with an External Broker instead of the embedded engine.

Hi @Rich_Puderbaugh, welcome to the forums! I believe zeebe-process-test doesn’t mock workers, and instead monitors for job activation calls and you can then complete or fail them as needed for your tests. Here’s an example from the repository:

I’m not personally very versed in zeebe-process-test, so hopefully that helps!

Hello Nathan,

Thanks for the welcome and the prompt advice.
What I am trying to do is test a BPMN process flow and I want mock the Service Task or JobWorker to conditionally mutate or add output variables depending on the input variables. I am trying all of this using either the ZeebeProcessTest annotation or using the EngineFactory to start the engine and create the client.

I tried creating a worker (not a mock in this case) directly from the zeebeClient as follows:

// Note this is in Kotlin not Java
zeebeClient.newWorker().jobType(“test-worker”).handler { client, job →

        val variables = job.variablesAsMap

        if (variables["color"] == "green") {
            variables["color"] = "yellow"
        } else if (variables["color"] == "yellow") {
            variables["color"] = "red"
        }

        client.newCompleteCommand(job).variables(variables)
            .send();
    }

Hi @Rich_Puderbaugh - you shouldn’t need to fully mock the job. zeebe-process-test is designed for you to write tests that walk through your model and assert each step is what you expect it to be. When it comes to a service task/job worker, you can fetch the current job and return the variables with the completion command. In the link provided, you can see that it fetches active jobs of a certain type, and around line #308 is where you would do your variable check.

(This is non-functional code, but demonstrates what I mean)

final var job = activateJobsResponse.getJobs().get(i);
var variables = job.getVariablesAsMap();
// do your variable checks/manipulations
client.newCompleteCommand(job.getKey()).variables(variables).send().join();

Hello @nathan.loding thank so much for the details and example.
However, I am not able to figure out how to acquire the ActivateJobsResponse to get the jobs without causing the test to try to connect to broker causing the deadline exceeded exception. Can you provide some sample code? I will want to do something similar in mocking connectors.

Thanks,

Rich

Hi @Rich_Puderbaugh - I’m not sure I follow. zeebe-process-test provides an embedded engine, and getting the active jobs involves connecting to that embedded engine. The difference is that zeebe-process-test is manually moving the process from task to task rather than the engine running it automatically.

I think the example tests in the zeebe-process-test repo demonstrate this pretty well. There is a resources folder next to the test that contain the BPMN models being tested so you can see how it works step by step.