我已经将Simple 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
的某些实现。
这里有没有要使用的哑剧?如何尽可能简单地进行测试?
我不会启动用于测试此代码的流程实例。 Google没有提供一些有用的东西。
答案 0 :(得分:1)
DelegateExecution
是一个接口,因此您可以实现自己的接口。但是更好的选择是使用诸如mockito之类的模拟库,该库允许您仅模拟您感兴趣的方法调用。
import static org.mockito.Mockito.*;
...
DelegateExecution mockExecution = mock(DelegateExecution.class);
doReturn("printJobId").when(mockExecution).getVariable(eq("VIP-Variable"));
cleanupVariables.execute(mockExecution);
以下是使用嘲讽进行模拟的教程: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>
但是我从来没有用过,所以我对此无能为力。