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}">
  <bpmn:extensionElements>
	<camunda:inputOutput>
	  <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>
	</camunda:inputOutput>
  </bpmn:extensionElements>
  <bpmn:incoming>SequenceFlow_1dsx1bm</bpmn:incoming>
  <bpmn:incoming>SequenceFlow_0mikm41</bpmn:incoming>
  <bpmn:outgoing>SequenceFlow_00yhmgb</bpmn:outgoing>
</bpmn:serviceTask>

The delegate implementation looks like this:

@Component("queueListInputDelegate")
public class QueueListInputDelegate implements JavaDelegate {
[...]
} 

And my test case looks like this:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = { TestConfiguration.class })
@ActiveProfiles("test")
public class QueueMoveMockTest {

	@Autowired
	private RuntimeService runtimeService;
	
	@Autowired
	private MockQueueListInputDelegate mockQueueListInputDelegate;
	
	@Test
	@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,
Kristian

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,
Kristian