我已经实现了一个简单的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
的一些实现。这里有没有可用的假动作?如何测试这个尽可能简单?
我不会启动一个用于测试此代码的processinstance。谷歌没有提出一些有用的东西。
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>
但我无法帮助,因为我从未使用它。