Search code examples
javaunit-testingdelegatesbpmncamunda

bpmn - simple test for JavaDelegate implementation


I have implement a Simple JavaDelegate as a Task of my BPMN-Process:

public class CleanupVariables implements JavaDelegate {

    @Override
    public void execute(DelegateExecution execution) throws Exception {
        String printJobId = execution.getVariable("VIP-Variable").toString();

        // remove all variables
        execution.removeVariables();

        // set variable
        execution.setVariable("VIP-Variable", printJobId);
    }
}

Now I want to write a Unit-Test.

 @Test
    public void testRemove() throws Exception {
        // Arrange
        CleanupVariables cleanupVariables = new CleanupVariables();

        testdelegate.setVariable("VIP-Variable", "12345");
        testdelegate.setVariable("foo", "bar");

        // Act
        cleanupVariables.execute(????); // FIXME what to insert here?

        // Assert
        Assertions.assertThat(testdelegate.getVariables().size()).isEqualTo(1);
        Assertions.assertThat(testdelegate.getVariable("VIP-Variable")).isEqualTo("12345");

    }

I could not figure out how to insert some implementation of DelegateExecution in my act-step. Is there any dummy-impl to use right here? How to test this as simple as possible?

I will not start a processinstance for testing this code. Google didn't come up with some usefull stuff.


Solution

  • DelegateExecution is an interface, so you can implement your own. But better option is to use some mocking library like mockito, which allows you to simulate only the method calls which you are interested in.

    import static org.mockito.Mockito.*;
    ...
    
    DelegateExecution mockExecution = mock(DelegateExecution.class);
    doReturn("printJobId").when(mockExecution).getVariable(eq("VIP-Variable"));
    cleanupVariables.execute(mockExecution);
    

    Here's a tutorial for mocking with mockito: https://www.baeldung.com/mockito-series

    Or maybe you can use DelegateExecutionFake which is in this package:

        <dependency>
            <groupId>org.camunda.bpm.extension.mockito</groupId>
            <artifactId>camunda-bpm-mockito</artifactId>
            <version>3.1.0</version>
            <scope>test</scope>
        </dependency>
    

    But I cannot help with that since I've never used it.