How to inject mocks of the Java Delegate fields?

Hello everyone,

I am using camunda-bpm-mockito extension to create unit tests for my business process.

I use the method Mocks.register to mock the Delegate class:

@Before
public void setUp() {
    init(processEngineRule.getProcessEngine());
    Mocks.register("delegate", Delegate.class); //This is the delegate class for the delegate expression
}

However in the delegate class I have the following fields:

public class Delegate implements JavaDelegate {
    private Object field1;
    private Object field2;
    // Some additional fields

    @Override
    void execute(DelegateExpression execution) throws Exception {
     // Does nothing for now
     return;
    }
}

How do I inject Mocks of the delegate fields (field1, field2)?

Thank you very much!

Are you able to design your delegate class to make this easier? Something like injection through the constructor (preferred) or getters/setters? If not, you may have to resort to reflection (something like PowerMock might help here).

1 Like

First of all thank you very much for the reply :slight_smile: In case I make the fields settable via constructor, would the Mock.register automatically mock the fields? I am interested in the feature @Mock for the delegate fields in case of Mockito.

No, I don’t think so. Looks like you’ll have a little extra work to do. Take a look at this example.

1 Like

Isn’t it so that you should specify instaces and not classes when registering mocks? Then you can configure your delegate mock instance however you like and register it.

Hi,

There are two approaches you should consider.
It is a good idea to to separate tests for the delegates from the tests of the process.

For the first case, you can use CamundaBPMMockito’s registerJavaDelegateMock method es described in the Readme. In this case you usually want to stub the delegate and test the process model.

For testing the delegate class itself, please write a Unit Test and use the delegateExecutionFake() to create a fake execution and use it in the test.

See the same readme.

http://camunda.github.io/camunda-bpm-mockito/

Cheers

Simon

1 Like