How to "mock" a servicetask expression in Camunda Platform Scenario

I started using Camunda Platform Scenario for testing my process. My process is embedded in a SpringBoot-App and contains ServiceTasks that are implemented in a Java class and referenced as Expression.

Example-Reference in the ServiceTask in the modeler:

Expression: ${mf.myMethod(execution)}

“mf” is a Spring Bean name. That Bean implements “myMethod”

public void myMethod(DelegateExecution execution) { ...}

Now to test different process paths, I have to mock either the whole “mf” Bean (and test it with normal UnitTests) or at least mock the dependencies of the “mf” Bean (count the Bean as part of the process and don’t test it individually).

Can I do this with Camunda Platform Scenario?

Thanks
Stephan

1 Like

I found a solution to mock the whole expression class. I can create a mock of the “mf” Bean and register it with Camunda

Mocks.register("mf", myMock);

Then I can use standard Mockito to mock the void methods and manipulate the passed DelegateExecution object as needed.

doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        ((DelegateExecution)args[0]).setVariable(myProcessVariable, value);
        return null; 
    }).when(myMock).myMethod(any());
1 Like

I guess I asked my question too early :slight_smile: It is even simpler to include the “mf” Bean in the Scenario test and mock the Dependencies of it.

I can use plain SprintBoot magic to mock the Bean dependency since the Scenario test is a @SpringBootTest

@MockBean
private ServiceBean dependecyOfExpressionClass;

and then use standard Mockito to define the desired behavior of the Bean used by the “mf” Expression Bean.

when(dependecyOfExpressionClass.whateverMethod()).thenReturn(whatever);
1 Like