Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
bpmn-JavaDelegate实现的简单测试_Java_Unit Testing_Delegates_Bpmn_Camunda - Fatal编程技术网

bpmn-JavaDelegate实现的简单测试

bpmn-JavaDelegate实现的简单测试,java,unit-testing,delegates,bpmn,camunda,Java,Unit Testing,Delegates,Bpmn,Camunda,我已经实现了一个简单的JavaDelegate作为BPMN流程的一项任务: public class CleanupVariables implements JavaDelegate { @Override public void execute(DelegateExecution execution) throws Exception { String printJobId = execution.getVariable("VIP-Variable").toS

我已经实现了一个简单的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");

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


我不会启动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进行模拟的教程:

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

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

org.camunda.bpm.extension.mockito
卡蒙达莫基托酒店
3.1.0
测验

但是我没用过,所以我没办法

在我看来,在给定的测试用例中模拟这个对象是错误的。如果我这样做,测试将只测试如果我模仿一切正确。遗憾的是,camunda没有提供这个接口的一些虚拟实现。我不认为这是错误的,在单元测试中,您只想在可能的情况下测试单个类。但我找到了一些可能有用的包裹。我自己不用它,但也许它能满足你的需求。