From 30053815a178bf599a6232984580b3e87780344e Mon Sep 17 00:00:00 2001 From: David Ellermann <48996172+DavidEllermannIkor@users.noreply.github.com> Date: Wed, 4 Oct 2023 17:31:34 +0200 Subject: [PATCH] Adding possibility to throw an error on execution (#327) Adding test for for throwing an error on execution Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Jan Galinski --- .../mockito/process/CallActivityMock.java | 16 ++++++++++ .../mockito/CallActivityMockExampleTest.java | 30 +++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/src/main/java/org/camunda/community/mockito/process/CallActivityMock.java b/src/main/java/org/camunda/community/mockito/process/CallActivityMock.java index d1e5797a..a4263831 100644 --- a/src/main/java/org/camunda/community/mockito/process/CallActivityMock.java +++ b/src/main/java/org/camunda/community/mockito/process/CallActivityMock.java @@ -39,6 +39,7 @@ public interface MockedModelConfigurer { private BpmnModelInstance modelInstance = null; private String escalation; + private String error; public CallActivityMock(final String processId, final MockedModelConfigurer modelConfigurer) { this.processId = processId; @@ -235,6 +236,19 @@ public CallActivityMock onExecutionThrowEscalation(final String escalationCode) this.escalation = escalationCode; return this; } + + /** + * On execution, the MockProcess will throw error for the given code when no escalation is set + * + * If called multiple times, this method adds only the last error to the end event. + * + * @param escalationCode the escalation code + * @return self + */ + public CallActivityMock onExecutionThrowError(final String errorCode) { + this.error = errorCode; + return this; + } /** * This will deploy the mock process. @@ -260,6 +274,8 @@ public BpmnModelInstance getModelInstance() { if (this.escalation != null) { endEvent = endEvent.escalation(this.escalation); + } else if (this.error != null) { + endEvent = endEvent.error(this.error); } modelInstance = endEvent.done(); diff --git a/src/test/java/org/camunda/community/mockito/CallActivityMockExampleTest.java b/src/test/java/org/camunda/community/mockito/CallActivityMockExampleTest.java index e27c3116..c636a7e5 100644 --- a/src/test/java/org/camunda/community/mockito/CallActivityMockExampleTest.java +++ b/src/test/java/org/camunda/community/mockito/CallActivityMockExampleTest.java @@ -260,6 +260,36 @@ public void register_subprocess_mock_throwEscalation() { isEnded(processInstance); assertEquals(escalationEndId, ((ProcessInstanceWithVariablesImpl) processInstance).getExecutionEntity().getActivityId()); } + + @Test + public void register_subprocess_mock_throwError() { + String errorCode = "SOME_ERROR"; + String subprocessId = "call_subprocess"; + String errorEndId = "ErrorEnd"; + + BpmnModelInstance processWithSubProcess = Bpmn.createExecutableProcess(PROCESS_ID) + .startEvent("start") + .callActivity(subprocessId) + .calledElement(SUB_PROCESS_ID) + .boundaryEvent() + .error(errorCode) + .endEvent(errorEndId) + .moveToActivity(subprocessId) + .userTask(TASK_USERTASK) + .endEvent("end") + .done(); + + camunda.manageDeployment(new DeployProcess(camunda).apply(PROCESS_ID, processWithSubProcess)); + + camunda.manageDeployment(registerCallActivityMock(SUB_PROCESS_ID) + .onExecutionThrowError(errorCode) + .deploy(camunda)); + + ProcessInstance processInstance = startProcess(PROCESS_ID); + + isEnded(processInstance); + assertEquals(errorEndId, ((ProcessInstanceWithVariablesImpl) processInstance).getExecutionEntity().getActivityId()); + } private void prepareProcessWithOneSubprocess() { final BpmnModelInstance processWithSubProcess = Bpmn.createExecutableProcess(PROCESS_ID)