Back to Repositories

Testing Dictionary Framework Utils Implementation in RuoYi Vue Pro

A comprehensive unit test suite for the DictFrameworkUtils class, focusing on dictionary data handling and label-value mapping functionality. The tests validate core dictionary operations and data parsing capabilities in the RuoYi Vue Pro framework.

Test Coverage Overview

The test suite provides thorough coverage of dictionary data operations.

Key areas tested include:
  • Dictionary data label retrieval
  • Value parsing from dictionary entries
  • Status validation for enabled dictionary items
  • Integration with DictDataApi service

Implementation Analysis

The testing approach utilizes Mockito for service simulation and JUnit for assertions. The implementation follows a clear pattern of mocking data, setting up expectations, and validating results. The tests leverage BaseMockitoUnitTest for consistent testing infrastructure and RandomUtils for test data generation.

Technical Details

Testing tools and configuration:
  • JUnit Jupiter for test execution
  • Mockito framework for mocking
  • Custom BaseMockitoUnitTest extension
  • RandomUtils for test data generation
  • BeforeEach setup for DictFrameworkUtils initialization

Best Practices Demonstrated

The test suite exemplifies several testing best practices including proper test isolation, clear test method naming, and comprehensive mock setup. Each test focuses on a single functionality with clear arrange-act-assert patterns. The use of randomized test data through RandomUtils enhances test robustness.

yunaiv/ruoyi-vue-pro

yudao-framework/yudao-spring-boot-starter-excel/src/test/java/cn/iocoder/yudao/framework/dict/core/util/DictFrameworkUtilsTest.java

            
package cn.iocoder.yudao.framework.dict.core.util;

import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.dict.core.DictFrameworkUtils;
import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest;
import cn.iocoder.yudao.module.system.api.dict.DictDataApi;
import cn.iocoder.yudao.module.system.api.dict.dto.DictDataRespDTO;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;

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

/**
 * {@link DictFrameworkUtils} 的单元测试
 */
public class DictFrameworkUtilsTest extends BaseMockitoUnitTest {

    @Mock
    private DictDataApi dictDataApi;

    @BeforeEach
    public void setUp() {
        DictFrameworkUtils.init(dictDataApi);
    }

    @Test
    public void testGetDictDataLabel() {
        // mock 数据
        DictDataRespDTO dataRespDTO = randomPojo(DictDataRespDTO.class, o -> o.setStatus(CommonStatusEnum.ENABLE.getStatus()));
        // mock 方法
        when(dictDataApi.getDictData(dataRespDTO.getDictType(), dataRespDTO.getValue())).thenReturn(dataRespDTO);

        // 断言返回值
        assertEquals(dataRespDTO.getLabel(), DictFrameworkUtils.getDictDataLabel(dataRespDTO.getDictType(), dataRespDTO.getValue()));
    }

    @Test
    public void testParseDictDataValue() {
        // mock 数据
        DictDataRespDTO resp = randomPojo(DictDataRespDTO.class, o -> o.setStatus(CommonStatusEnum.ENABLE.getStatus()));
        // mock 方法
        when(dictDataApi.parseDictData(resp.getDictType(), resp.getLabel())).thenReturn(resp);

        // 断言返回值
        assertEquals(resp.getValue(), DictFrameworkUtils.parseDictDataValue(resp.getDictType(), resp.getLabel()));
    }

}