Junit tests for ZeebeWorker

@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);

					log.info("Task1 Service is completed");
				} else {
					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);

This is the method for which I have to provide Junit tests
Please provide example for reference as well with ZeebeWorker

Hello @Prashik_Hingaspure ,

you can test your worker using zeebe-process-test:

There is a very good description in the readme. Anyway, I would unit-test the process (if it belongs to the project) together with the code.

I hope this helps


1 Like

When process instance started for normal scenario all worker associated with bpmn process model automatically invoked. Is this functionality available for testing or I have to explicitly create ActivateJob through zeebe client?
When I refer above github repo that you have shared, its only testing process with single worker. What if I have 10 or 100 workers in a process to test each one I have to create separate processes (one per worker) having one worker each.

Hello @Prashik_Hingaspure ,

the idea is to run through the process “manually”, meaning you start it, then you assert that you are at some point, then you execute the job that exists using your (mocked) business logic. Then, you make assertions on where you are and so on.

Every step where interaction from outside is required to drive the process will be asserted. This enables testing of the desired behaviour in depth.