bpmn - JavaDelegate 实现的简单测试

bpmn - simple test for JavaDelegate implementation

我已经实现了一个简单的 JavaDelegate 作为我的 BPMN 流程的任务:

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);
    }
}

现在我想写一个单元测试。

 @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");

    }

我不知道如何在我的行动步骤中插入一些 DelegateExecution 的实现。 有没有可以在这里使用的虚拟实现?如何尽可能简单地进行测试?

我不会启动用于测试此代码的进程实例。 Google 没有想出一些有用的东西。

DelegateExecution 是一个接口,因此您可以实现自己的接口。但更好的选择是使用像 mockito 这样的模拟库,它允许你只模拟你感兴趣的方法调用。

import static org.mockito.Mockito.*;
...

DelegateExecution mockExecution = mock(DelegateExecution.class);
doReturn("printJobId").when(mockExecution).getVariable(eq("VIP-Variable"));
cleanupVariables.execute(mockExecution);

这是一个使用 mockito 进行模拟的教程:https://www.baeldung.com/mockito-series

或者您可以使用此包中的 DelegateExecutionFake

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

但我没办法解决这个问题,因为我从未使用过它。