Back to Repositories

Testing GoView Data Service SQL Processing in RuoYi Vue Pro

This test suite validates the GoView data service implementation in the RuoYi Vue Pro system, focusing on SQL data retrieval and response formatting. The tests verify the service’s ability to execute SQL queries and transform database results into structured view responses.

Test Coverage Overview

The test suite provides comprehensive coverage of the GoView data service functionality.

Key areas tested include:
  • SQL query execution via JdbcTemplate
  • Result set metadata handling
  • Data transformation from SQL results to GoView response format
  • Column dimension mapping
  • Multi-row result processing

Implementation Analysis

The testing approach utilizes Mockito for database interaction simulation and JUnit for assertions. The implementation follows a structured pattern of mocking the JdbcTemplate, SqlRowSet, and metadata components to validate the complete data retrieval and transformation pipeline.

Framework-specific features include:
  • Spring Boot test configuration
  • BaseDbUnitTest extension
  • Mockito bean injection

Technical Details

Testing tools and configuration:
  • JUnit Jupiter for test execution
  • Mockito for mocking database interactions
  • Spring Boot Test for dependency injection
  • Custom BaseDbUnitTest for database testing support
  • @Import annotation for component configuration

Best Practices Demonstrated

The test implementation showcases several testing best practices including proper isolation of database operations, comprehensive mock setup, and thorough result validation.

Notable practices include:
  • Structured test arrangement using AAA pattern (Arrange-Act-Assert)
  • Detailed mock configuration for complex objects
  • Comprehensive assertion of response structure
  • Clear separation of test setup and verification

yunaiv/ruoyi-vue-pro

yudao-module-report/yudao-module-report-biz/src/test/java/cn/iocoder/yudao/module/report/service/goview/GoViewDataServiceImplTest.java

            
package cn.iocoder.yudao.module.report.service.goview;

import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
import cn.iocoder.yudao.module.report.controller.admin.goview.vo.data.GoViewDataRespVO;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.jdbc.support.rowset.SqlRowSetMetaData;

import javax.annotation.Resource;
import java.util.Arrays;

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;

@Import(GoViewDataServiceImpl.class)
public class GoViewDataServiceImplTest extends BaseDbUnitTest {

    @Resource
    private GoViewDataServiceImpl goViewDataService;

    @MockBean
    private JdbcTemplate jdbcTemplate;

    @Test
    public void testGetDataBySQL() {
        // 准备参数
        String sql = "SELECT id, name FROM system_users";
        // mock 方法
        SqlRowSet sqlRowSet = mock(SqlRowSet.class);
        when(jdbcTemplate.queryForRowSet(eq(sql))).thenReturn(sqlRowSet);
        // mock 元数据
        SqlRowSetMetaData metaData = mock(SqlRowSetMetaData.class);
        when(sqlRowSet.getMetaData()).thenReturn(metaData);
        when(metaData.getColumnNames()).thenReturn(new String[]{"id", "name"});
        // mock 数据明细
        when(sqlRowSet.next()).thenReturn(true).thenReturn(true).thenReturn(false);
        when(sqlRowSet.getObject("id")).thenReturn(1L).thenReturn(2L);
        when(sqlRowSet.getObject("name")).thenReturn("芋道源码").thenReturn("芋道");

        // 调用
        GoViewDataRespVO dataBySQL = goViewDataService.getDataBySQL(sql);
        // 断言
        assertEquals(Arrays.asList("id", "name"), dataBySQL.getDimensions());
        assertEquals(2, dataBySQL.getDimensions().size());
        assertEquals(2, dataBySQL.getSource().get(0).size());
        assertEquals(1L, dataBySQL.getSource().get(0).get("id"));
        assertEquals("芋道源码", dataBySQL.getSource().get(0).get("name"));
        assertEquals(2, dataBySQL.getSource().get(1).size());
        assertEquals(2L, dataBySQL.getSource().get(1).get("id"));
        assertEquals("芋道", dataBySQL.getSource().get(1).get("name"));
    }

}