测试流程依赖性(中)

测试流程依赖性(中)

 

在测试中模拟交付过程

为此,我们在defaultScenario()方法中添加以下内容:

ProcessExpressions.registerCallActivityMock(DELIVERY_PROCESS_KEY)

                .deploy(rule);

 

        when(testOrderProcess.runsCallActivity(TASK_DELIVER_ORDER1))

                .thenReturn(Scenario.use(deliveryRequest));



在这种情况下,shouldExecuteOrderCancelled我们必须调整Call Activity Mock的行为以在执行过程中引发错误:

ProcessExpressions.registerCallActivityMock(DELIVERY_PROCESS_KEY)

                .onExecutionDo(execution -> {

                    throw new BpmnError("deliveryFailed");

                })

                .deploy(rule);



我们已经完成了为我们的定购流程定义不同变体的工作–非常简单!使用camunda-bpm-mockito可以做更多的事情,只需尝试一下。

为交付过程编写单独的测试

接下来,我们为交付过程创建一个单独的测试类,并采用shouldExecuteOrderCancelled和shouldExecuteDeliverTwice方法。

@Deployment(resources = "deliver-process.bpmn")

public class DeliveryProcessTest {

 

    public static final String PROCESS_KEY = "deliveryprocess";

    public static final String TASK_DELIVER_ORDER = "Task_DeliverOrder";

    public static final String VAR_ORDER_DELIVERED = "orderDelivered";

    public static final String END_EVENT_DELIVERY_COMPLETED = "EndEvent_DeliveryCompleted";

    public static final String END_EVENT_DELIVERY_CANCELLED = "EndEvent_DeliveryCancelled";

 

    @Rule

    public ProcessEngineRule rule = new ProcessEngineRule();

 

    @Mock

    private ProcessScenario testDeliveryProcess;

 

    @Before

    public void defaultScenario() {

        MockitoAnnotations.initMocks(this);

 

        //Happy-Path

        when(testDeliveryProcess.waitsAtUserTask(TASK_DELIVER_ORDER))

                .thenReturn(task -> {

                    task.complete(withVariables(VAR_ORDER_DELIVERED, true));

                });

    }

 

    @Test

    public void shouldExecuteHappyPath() {

        Scenario.run(testDeliveryProcess)

                .startByKey(PROCESS_KEY)

                .execute();

 

        verify(testDeliveryProcess)

                .hasFinished(END_EVENT_DELIVERY_COMPLETED);

    }

 

    @Test

    public void shouldExecuteOrderCancelled() {

        when(testDeliveryProcess.waitsAtUserTask(TASK_DELIVER_ORDER)).thenReturn(task -> {

            taskService().handleBpmnError(task.getId(), "DeliveryCancelled");

        });

 

        Scenario.run(testDeliveryProcess)

                .startByKey(PROCESS_KEY)

                .execute();

 

        verify(testDeliveryProcess)

                .hasFinished(END_EVENT_DELIVERY_CANCELLED);

    }

 

    @Test

    public void shouldExecuteDeliverTwice() {

        when(testDeliveryProcess.waitsAtUserTask(TASK_DELIVER_ORDER)).thenReturn(task -> {

            task.complete(withVariables(VAR_ORDER_DELIVERED, false));

        }, task -> {

            task.complete(withVariables(VAR_ORDER_DELIVERED, true));

        });

 

        Scenario.run(testDeliveryProcess)

                .startByKey(PROCESS_KEY)

                .execute();

 

        verify(testDeliveryProcess, times(2))

                .hasCompleted(TASK_DELIVER_ORDER);

        verify(testDeliveryProcess)

                .hasFinished(END_EVENT_DELIVERY_COMPLETED);

    }

}

 

相关教程