Unit test external task

Hi guys!

I´m doing unit test with external task


@Deployment(resources = “SAC0002-Sincronizacao-de-arvore-de-classificacao.bpmn”)
public void externalTask2() {
RuntimeService runtimeService = processEngine.getRuntimeService();//
ProcessInstance proccessInstance = runtimeService.startProcessInstanceByKey(“SAC0002SincronizacaoDeArvoreDeClassificacaoProcess”);

	Map<String, Object> mapa = new HashMap<String, Object>();
	mapa.put("enterprise", "1");
	mapa.put("origin", "1");
	mapa.put("type", "1");
	complete(externalTask(), mapa);
	new AtualizarArvoreDeClassificacaoTask();

but do not enter the method handle, I have the mock base url or ExternalTaskClient?

public AtualizarArvoreDeClassificacaoTask() {
log.info(“AtualizarArvoreDeClassificacaoTask() - start”);
ExternalTaskClient client = ExternalTaskClient.create().baseUrl(CamundaRestApi.url())

	client.subscribe("AtualizarArvoreDeClassificacaoTask").handler((externalTask, externalTaskService) -> {
		log.info("AtualizarArvoreDeClassificacaoTask() - start handler");
		Map<String, Object> variables = externalTask.getAllVariables();
		Enterprise enterprise = (Enterprise) variables.get("enterprise");
		String origin = (String) variables.get("origin");
		ManifestationType type = (ManifestationType) variables.get("type");
		ClassificationTree tree = new ClassificationTree(enterprise, origin, type);
		ClassificationTree existentTree = repository.findByCompositeCode(enterprise, origin, type);
		if (existentTree != null) {
			if (!tree.equals(existentTree)) {
				tree = repository.save(tree);
		} else {
			tree = repository.save(tree);

		variables.put("tree", tree);

		externalTaskService.complete(externalTask, variables);
		log.info("AtualizarArvoreDeClassificacaoTask() - end handler");
	log.info("AtualizarArvoreDeClassificacaoTask() - end");

Hi @fernando123,

the task of the unit test you posted is to test the process logic without running trough the implementation of the worker. It’s mocking the worker.

So, it works as expected.

Hope this helps, Ingo

Hi @Ingo_Richtsmeier,

How can I create a worker in my process?


Hi @fernando123,

you cannot create a worker in the test.

But you can refactor your worker and extract the body of the lambda function (the handler) into a method. Then you can invoke the method in the JUnit test.

Hope this helps, Ingo

May you give an example in java code?

Anyone know this?