我已经实现了一个简单的JavaDelegate作为我的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);
}
}
现在我想写一个单元测试。
@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
的实现。这里有假人可以使用吗?如何尽可能简单地测试它?
我不会启动用于测试此代码的流程实例。谷歌没有想出一些有用的东西。
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>
但我无能为力,因为我从未使用过它。