Junit for custom JavaDelegate

I have a custom JavaDelgate class and i would like to add junit test case for the execute method. Do we have any samples for the same ?

Hi @jothianand

You could write a junit test which deploys a test process that uses your custom JavaDelegate. With that approach you have a proper integration test.

@Test
@Deployment(resources = "test-process.bpmn20.xml")
public void test() {
ProcessInstance processInstance = runtimeService.createProcessInstanceBuilder()
                .processDefinitionKey("testProcess")
                .start();
}

test-process.bpmn20.xml:

<process id="testProcess" name="testProcess" isExecutable="true">
    <startEvent id="start" />
    <endEvent id="end"/>
    <serviceTask id="serviceTask" flowable:delegateExpression="${yourJavaDelegate}"/>
    <sequenceFlow id="flow1" sourceRef="start" targetRef="serviceTask"/>
    <sequenceFlow id="flow2" sourceRef="serviceTask" targetRef="end"/>
  </process>

Regards,
Simon

Thanks Simon.

QQ on the test case you shared, what exactly do we assert here ? Could you please share the complete test case if you have.

Also i’m more interested on unit tests for each component. Like, if we have a bunch of listeners, delegates, etc. in our process, then how can we write unit test cases for each one of them.

Regards,
Jothianand S

try to use archetype to generate a project:

or have a look into flowable source, there are plenty of jUnit tests.

For instance if your Java Delegate adds a variable to the process, you could test it afterwards:


public class YourJavaDelegate implements JavaDelegate {
    @Override
    public void execute(DelegateExecution execution) {
        execution.setVariable("foo", true);
    }
}
@Test
@Deployment(resources = "test-process.bpmn20.xml")
public void test() {
ProcessInstance processInstance = runtimeService.createProcessInstanceBuilder()
                .processDefinitionKey("testProcess")
                .start();

assertThat((Boolean) runtimeService.getVariable(processId, "foo")).isTrue();
}

Thanks Martin and Simon, will try these out.