Back to Repositories

Testing BPM Task Candidate Department Leader Strategy in RuoYi Vue Pro

A comprehensive unit test suite for the BPM Task Candidate Department Leader Strategy implementation in the RuoYi Vue Pro framework. This test validates the department leader user calculation logic for business process management tasks.

Test Coverage Overview

The test suite provides targeted coverage for the department leader user calculation functionality.

Key areas tested include:
  • Department ID parameter parsing
  • Leader user ID retrieval for multiple departments
  • Integration with DeptApi service
  • Validation of returned user ID sets

Implementation Analysis

The testing approach utilizes Mockito for dependency isolation and JUnit Jupiter for test execution. The implementation demonstrates proper mocking patterns for external API calls and validates the business logic for department leader resolution.

The test leverages BaseMockitoUnitTest for common testing infrastructure and employs RandomUtils for test data generation.

Technical Details

Testing tools and configuration:
  • JUnit Jupiter test framework
  • Mockito mocking framework
  • AssertJ assertions library
  • Custom RandomUtils for test data generation
  • BaseMockitoUnitTest base class for common setup

Best Practices Demonstrated

The test exhibits several quality testing practices:

  • Clear test method naming and organization
  • Proper separation of test setup, execution, and verification
  • Effective use of mocking to isolate dependencies
  • Comprehensive assertion of expected results
  • 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/dept/BpmTaskCandidateDeptLeaderStrategyTest.java

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

import cn.iocoder.yudao.framework.common.util.collection.SetUtils;
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 java.util.Set;

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

public class BpmTaskCandidateDeptLeaderStrategyTest extends BaseMockitoUnitTest {

    @InjectMocks
    private BpmTaskCandidateDeptLeaderStrategy strategy;

    @Mock
    private DeptApi deptApi;

    @Test
    public void testCalculateUsers() {
        // 准备参数
        String param = "10,20";
        // mock 方法
        when(deptApi.getDeptList(eq(SetUtils.asSet(10L, 20L)))).thenReturn(asList(
                randomPojo(DeptRespDTO.class, o -> o.setId(10L).setParentId(10L).setLeaderUserId(11L)),
                randomPojo(DeptRespDTO.class, o -> o.setId(20L).setParentId(20L).setLeaderUserId(21L))));

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

}