Mocks.register() not working with delegate expression

Dear all,

I’m trying to run an integration test using Spring Boot where I replace one of the service tasks with a mock class. I’ve tried to use various keys for Mocks.register() but when running the test case the process engine always calls the real delegate implementation instead of the mock.

My service task definition looks like this:

<bpmn:serviceTask id="QueueListInputTask" name="Queue list input" camunda:modelerTemplate="QueueListInput" camunda:delegateExpression="${queueListInputDelegate}">
	  <camunda:inputParameter name="SourceQueueOffice">XXXXXXXXX</camunda:inputParameter>
	  <camunda:inputParameter name="SourceQueueNumber">1{SourceQueueNumber}</camunda:inputParameter>
	  <camunda:inputParameter name="SourceQueueCategory">0</camunda:inputParameter>
	  <camunda:inputParameter name="SourceQueueDateRange"></camunda:inputParameter>

The delegate implementation looks like this:

public class QueueListInputDelegate implements JavaDelegate {

And my test case looks like this:

@SpringBootTest(classes = { TestConfiguration.class })
public class QueueMoveMockTest {

	private RuntimeService runtimeService;
	private MockQueueListInputDelegate mockQueueListInputDelegate;
	@Deployment(resources = { "QueueMove.bpmn" })
	public void QueueMoveMockExample() {
		Mocks.register("queueListInputDelegate", mockQueueListInputDelegate);
		Mocks.register(QueueListInputDelegate.class.getCanonicalName(), mockQueueListInputDelegate);
		ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("QueueMove", processVariables);


When using a service task definition with type camunda:class="…QueueListInputDelegate" without autowiring the mock is called. But when using this approach the autowiring in the delegate is not working as the delegate is not managed by Spring.

Right now my workaround is to use a separate Spring profile to replace the queueListInputDelegate bean with the mock bean. However I would like to specify the mock for the process definition directly inside the test case.

Is this possible?

Thanks in advance.

Kind regards,

Mocks.register() only works when you use the MockExpressionManager. With Spring you have the SpringExpressionManager and thus Mocks.register is never used.
So setting up a Spring context where you replace is generally the right idea. What you might be missing is the “MockBean” annotaion from Spring-test, this allows you to replace a bean with a mock directly inside the test.

1 Like

Hello Jan,

Thank you very much for the information. I’ve switched to @MockBean to override the delegates and it works great.

Kind regards,