How do I Unit test external task with attached error boundary event?

I have the following BPMN with error boundary event attached to the external task.

I was able to unit test for positive case completing external task normally with below code snippet.



import static org.camunda.bpm.engine.test.assertions.bpmn.BpmnAwareTests.assertThat;
import static org.camunda.bpm.engine.test.assertions.bpmn.BpmnAwareTests.complete;
import static org.camunda.bpm.engine.test.assertions.bpmn.BpmnAwareTests.externalTask;
import static org.junit.Assert.assertNotNull;

import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.camunda.bpm.engine.test.Deployment;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;

import com.test.Main;

@RunWith(SpringRunner.class)
@Deployment(resources = { "processes/external-task-test.bpmn" })
@SpringBootTest(classes = { Main.class }, properties = {
		"camunda.bpm.process-engine-name=default", "camunda.bpm.generate-unique-process-application-name=true",
		"spring.datasource.generate-unique-name=true", })
@ActiveProfiles({ "test" })
public class ExternalTaskProcessTest {

	@Autowired
	RuntimeService runtimeService;

	
	@Test
	public void testWorkflowExecution() {
		ProcessInstance pi = runtimeService.startProcessInstanceByKey("external-task");
		assertNotNull("processInstance", pi);
		assertThat(pi).isWaitingAt("external-task").externalTask().hasTopicName("fetchdata");
		complete(externalTask());
		assertThat(pi).isEnded();
	}

}

I would like to know how to do a unit test when an external task throws an error event ?

Attaching bpmn for refernce external-task-test.bpmn (3.9 KB)

Thanks,
Pradeep

I did some investigation found out a way to do it.

	@Test
	public void testWorkflowExecutionErrorEvent() {
		ProcessInstance pi = this.startWf();
		assertNotNull("processInstance", pi);
		//assertThat(pi).isWaitingAt('external-task').externalTask().hasTopicName("fetchdata");
		List<LockedExternalTask> lockedTasks = externalTaskService.fetchAndLock(1, "anonymousWorker")
				.topic("fetchdata", 30L * 1000L).execute();
		externalTaskService.handleBpmnError(lockedTasks.get(0).getId(), lockedTasks.get(0).getWorkerId(),
				"failed-fetch-data");
		assertThat(pi).isActive();
		assertThat(pi).isWaitingAt("failed-fetch-data").task().isAssignedTo("admin");
		complete(task());
		assertThat(pi).isEnded();
	}
1 Like