Back to Repositories

Testing Department Leader Candidate Strategy in RuoYi-Vue Pro

A comprehensive unit test suite for validating the department leader candidate strategy in the RuoYi-Vue Pro BPM module. This test suite ensures proper calculation of user IDs based on department hierarchy and leadership structure.

Test Coverage Overview

The test suite focuses on the BpmTaskCandidateDeptLeaderMultiStrategy class, specifically testing the calculation of eligible department leaders.

  • Tests department leader ID resolution
  • Validates multi-level department hierarchy handling
  • Verifies correct parameter parsing for department IDs
  • Ensures proper collection of leader user IDs

Implementation Analysis

The testing approach utilizes Mockito for dependency isolation and behavior simulation. The implementation leverages JUnit 5 features with BaseMockitoUnitTest extension.

  • Employs mock responses for DeptApi calls
  • Uses Answer interface for dynamic mock responses
  • Implements linked hash set comparisons for ordered results

Technical Details

  • Testing Framework: JUnit Jupiter
  • Mocking Framework: Mockito
  • Assertion Library: AssertJ
  • Base Test Class: BaseMockitoUnitTest
  • Test Utilities: RandomUtils for test data generation

Best Practices Demonstrated

The test class exemplifies clean testing practices with clear arrangement of test components and proper isolation of dependencies.

  • Clear test method organization (Arrange-Act-Assert pattern)
  • Effective use of mocking for external dependencies
  • Precise assertions for expected outcomes
  • Proper test data generation and parameter handling

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/BpmTaskCandidateDeptLeaderMultiStrategyTest.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.system.api.dept.DeptApi;
import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO;
import org.assertj.core.util.Sets;
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.Mockito.when;

public class BpmTaskCandidateDeptLeaderMultiStrategyTest extends BaseMockitoUnitTest {

    @InjectMocks
    private BpmTaskCandidateDeptLeaderMultiStrategy strategy;

    @Mock
    private DeptApi deptApi;

    @Test
    public void testCalculateUsers() {
        // 准备参数
        String param = "10,20|2";
        // mock 方法
        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));
        });

        // 调用
        Set<Long> userIds = strategy.calculateUsers(param);
        // 断言结果
        assertEquals(Sets.newLinkedHashSet(11L, 1001L, 21L, 2001L), userIds);
    }

}