Hi Camunda Team,
I’m writing service-tasks tests using JUnit5 and I’m trying to figure out how a serviceTask attributed by camunda:delegateExpression
is mapped to the corresponding java class that implements JavaDelegate
.
Whenever I try to run my tests I get this error: Unknown property used in expression: ${myServiceTask1}. Cause: Cannot resolve identifier 'myServiceTask1'
I’m likely to be missing something. I’ve tried referring to examples in which so far none has seem to solve my use-case as most are for userTask
which won’t require delegate expressions.
By the end of this I should be able to write integration tests as well without the same error.
test class
@ExtendWith({ProcessEngineExtension.class})
@Deployment(resources = {"my_sample_bpmn.bpmn"})
class MyProcessTest {
ProcessEngine engine;
private final Map<String, Object> variables = new HashMap<String, Object>();
@Test
void shouldExecuteProcess() {
variables.put("expected_input", "input_me");
RuntimeService runtimeService = engine.getRuntimeService();
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("my_process", variables);
Execution execution = runtimeService.createExecutionQuery()
.processInstanceId(processInstance.getId())
.activityId("my_service_task_1")
.singleResult();
assertThat(processInstance).isActive();
assertThat(String.valueOf(runtimeService.getVariable(execution.getId(), "expected_output_variable"))).isEqualTo("output_variable_value");
}
}
bpmn snippet
<bpmn:serviceTask id="my_service_task_1" name="My Service Task 1" camunda:delegateExpression="${myServiceTask1}">
<bpmn:extensionElements>
<camunda:inputOutput>
<camunda:inputParameter name="expected_input" />
<camunda:outputParameter name="Output_0bmeg48">"${expected_output_variable}"</camunda:outputParameter>
</camunda:inputOutput>
</bpmn:extensionElements>
<bpmn:incoming>Flow_1yvnhzn</bpmn:incoming>
<bpmn:outgoing>Flow_0r4ejnr</bpmn:outgoing>
</bpmn:serviceTask>```
Service class
@Component
@RequiredArgsConstructor
@Named("myServiceTask1")
public class ServiceTaskAdaptor implements JavaDelegate {
@Override
public void execute(DelegateExecution execution) throws Exception {
String input = (String) execution.getVariable("expected_input");
boolean outputVariableValue = getOutput(input);
execution.setVariable("expected_output_variable", outputVariableValue);
}
}