Back to Repositories

Testing BPM Task Candidate User Strategy in RuoYi-Vue-Pro

A comprehensive unit test suite for the BPM task candidate user strategy implementation in the RuoYi Vue Pro framework. This test suite validates the logic for determining task candidates based on process start users and activity parameters.

Test Coverage Overview

The test suite provides thorough coverage of the BpmTaskCandidateStartUserStrategy class functionality.

Key areas tested include:
  • Process instance start user retrieval
  • Task-based user calculation
  • Activity-based user calculation
  • Integration with Flowable execution context

Implementation Analysis

The testing approach utilizes Mockito for dependency isolation and JUnit Jupiter for test execution. The implementation follows a clear arrange-act-assert pattern, with careful mocking of the Flowable engine components and process instance service.

Framework-specific features leveraged include:
  • Mockito InjectMocks and Mock annotations
  • BaseMockitoUnitTest extension
  • Flowable DelegateExecution mocking

Technical Details

Testing tools and configuration:
  • JUnit Jupiter test framework
  • Mockito mocking framework
  • AssertJ assertions library
  • Custom BaseMockitoUnitTest base class
  • Flowable BPM engine test dependencies

Best Practices Demonstrated

The test suite exemplifies several testing best practices and quality patterns.

Notable practices include:
  • Clear test method naming conventions
  • Proper separation of test setup and assertions
  • Effective use of mocking to isolate dependencies
  • Comprehensive verification of business logic
  • Clean and maintainable test structure

yunaiv/ruoyi-vue-pro

yudao-module-bpm/yudao-module-bpm-biz/src/test/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/candidate/strategy/user/BpmTaskCandidateStartUserStrategyTest.java

            
package cn.iocoder.yudao.module.bpm.framework.flowable.core.candidate.strategy.user;

import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest;
import cn.iocoder.yudao.module.bpm.service.task.BpmProcessInstanceService;
import org.assertj.core.util.Sets;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.runtime.ProcessInstance;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;

import java.util.Set;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class BpmTaskCandidateStartUserStrategyTest extends BaseMockitoUnitTest {

    @InjectMocks
    private BpmTaskCandidateStartUserStrategy strategy;

    @Mock
    private BpmProcessInstanceService processInstanceService;

    @Test
    public void testCalculateUsersByTask() {
        // 准备参数
        String param = "2";
        // mock 方法(获得流程发起人)
        Long startUserId = 1L;
        ProcessInstance processInstance = mock(ProcessInstance.class);
        DelegateExecution execution = mock(DelegateExecution.class);
        when(processInstanceService.getProcessInstance(eq(execution.getProcessInstanceId()))).thenReturn(processInstance);
        when(processInstance.getStartUserId()).thenReturn(startUserId.toString());

        // 调用
        Set<Long> userIds = strategy.calculateUsersByTask(execution, param);
        // 断言
        assertEquals(Sets.newLinkedHashSet(startUserId), userIds);
    }

    @Test
    public void testCalculateUsersByActivity() {
        // 准备参数
        Long startUserId = 1L;

        // 调用
        Set<Long> userIds = strategy.calculateUsersByActivity(null, null, null,
                startUserId, null, null);
        // 断言
        assertEquals(Sets.newLinkedHashSet(startUserId), userIds);
    }

}