Mocking internal context variables

373 views
Skip to first unread message

Tina

unread,
Nov 10, 2014, 4:42:30 PM11/10/14
to camunda-...@googlegroups.com
Hi, 

Is it possible to user Mockito and org.camunda.bpm.engine.test.mock.Mocks to mock internal context variables such as "execution.getVariable("")" and such?
I've tried 
 DelegateExecution execution = mock(DelegateExecution.class);
        when(execution.getVariable("variable")).thenReturn(MOCK);
        Mocks.register("execution", execution);

with a field injection call: 
<camunda:field name="variable">
          <camunda:expression>${execution.getVariable("variable")}</camunda:expression>
        </camunda:field>

for a service task. 

But so far process engine has not been able to register the mock and still uses the actual value instead.

Tina 
Tnx 

thorben....@camunda.com

unread,
Nov 11, 2014, 5:25:51 AM11/11/14
to camunda-...@googlegroups.com
Hi Tina,

I looked at the code and can confirm the behavior.
To use the Mocks class, you have to configure another ExpressionManager, namely org.camunda.bpm.engine.test.mock.MockExpressionManager. Even if you have that, the expression won't resolve to your mock, because the MockExpressionManager registers the mock resolver after the variable scope resolver. What you could do to work around this, is to create a custom ExpressionManager that does the same as MockExpressionManager but registers the ELResolvers in the correct order.

Still, I think using the Mocks class is probably not a good option anyway because you only mock a very tiny fraction of the engine but other than that use the engine as it is. This is quite fragile since now there are two kinds of executions: the one you mocked and the one that the engine uses internally. Depending on your processes, your mock may not represent the actual engine state.

If all you want to do is test a JavaDelegate implementation in isolation, I would advise to mock DelegateExecution and explicitly call JavaDelegate#execute(DelegateExecution) in your test code. In order to have this working with field expressions, you would have to set them beforehand, for example by adding setters for the expressions and mocking them.

In a nutshell: Use mocks when you test a delegate in isolation. When you need the engine for an "integration test" of the delegate in the actual process, do not mock but set the variable explicitly on the execution.

Cheers,
Thorben
Reply all
Reply to author
Forward
0 new messages