Unit Test for Delegate Classes using delegate Expression with JUnit5

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

Hello,
first, what you do is not a service task test, but rather a process test. A task test would just instantiate the delegate class (possibly providing mocked dependencies), call the ‘execute’ method and check the results.

If you still want to do a process test, you have two options. Either load the spring context so that the references can be resolved or use the camunda mocks lib which effectively mocks the java delegate beans (it lets you to register your own instances).

Hi @fml2, thanks for the reply. Do you mind providing an illustrative example?

I can’t provide an example since I don’t know what you want to do. There are many examples for both in the internet, you just have to search.

@extusm any solutions?