Back to Repositories

Testing Department Leader Candidate Strategy in ruoyi-vue-pro

This test suite evaluates the BpmTaskCandidateStartUserDeptLeaderMultiStrategy class, which handles department leader candidate selection in a business process management workflow. It verifies the logic for calculating user IDs based on task and activity parameters, focusing on multi-department leader scenarios.

Test Coverage Overview

The test suite provides comprehensive coverage of the department leader candidate selection strategy.

Key areas tested include:
  • User calculation based on task execution context
  • User calculation based on activity parameters
  • Multi-department leader hierarchy traversal
  • Integration with process instance and user services

Implementation Analysis

The testing approach utilizes Mockito for dependency isolation and JUnit for test execution. The implementation employs a mock-based strategy to simulate process instance, user API, and department API interactions.

Notable patterns include:
  • Mock injection for external service dependencies
  • Controlled test data generation
  • Hierarchical department structure simulation

Technical Details

Testing infrastructure includes:
  • JUnit Jupiter for test execution
  • Mockito for mock object creation and verification
  • BaseMockitoUnitTest as the base test class
  • Custom utility methods for test data generation
  • AssertJ for assertions

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Clear test method naming conventions
  • Proper separation of test setup and execution
  • Effective use of mock objects for external dependencies
  • Reusable mock setup methods
  • Precise assertions for expected outcomes

yunaiv/ruoyi-vue-pro

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

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

import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest;
import cn.iocoder.yudao.module.bpm.service.task.BpmProcessInstanceService;
import cn.iocoder.yudao.module.system.api.dept.DeptApi;
import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
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 org.mockito.stubbing.Answer;

import java.util.Set;

import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class BpmTaskCandidateStartUserDeptLeaderMultiStrategyTest extends BaseMockitoUnitTest {

    @InjectMocks
    private BpmTaskCandidateStartUserDeptLeaderMultiStrategy strategy;

    @Mock
    private BpmProcessInstanceService processInstanceService;

    @Mock
    private AdminUserApi adminUserApi;
    @Mock
    private DeptApi deptApi;

    @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());
        // mock 方法(获取发起人的 multi 部门负责人)
        mockGetStartUserDept(startUserId);

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

    @Test
    public void testCalculateUsersByActivity() {
        // 准备参数
        String param = "2";
        // mock 方法
        Long startUserId = 1L;
        mockGetStartUserDept(startUserId);

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

    private void mockGetStartUserDept(Long startUserId) {
        when(adminUserApi.getUser(eq(startUserId))).thenReturn(
                randomPojo(AdminUserRespDTO.class, o -> o.setId(startUserId).setDeptId(10L)));
        when(deptApi.getDept(any())).thenAnswer((Answer<DeptRespDTO>) invocationOnMock -> {
            Long deptId = invocationOnMock.getArgument(0);
            return randomPojo(DeptRespDTO.class, o -> o.setId(deptId).setParentId(deptId * 100).setLeaderUserId(deptId + 1));
        });
    }

}