Back to Repositories

Testing Local File Storage Operations in RuoYi Vue Pro

This test suite validates the functionality of the LocalFileClient class for handling local file operations in the RuoYi Vue Pro framework. It focuses on testing file upload, path generation, and deletion capabilities for local file storage implementation.

Test Coverage Overview

The test suite covers core file handling operations including file upload, path management, and cleanup.

Key functionality tested includes:
  • Client initialization with configuration
  • File upload with path generation
  • Content type handling
  • File deletion operations
  • URL path construction for access

Implementation Analysis

The testing approach utilizes JUnit 5 for unit testing the LocalFileClient implementation. The test demonstrates a practical workflow using Hutool utilities for resource handling and ID generation.

The test pattern follows a setup-execute-verify structure with configuration initialization, file operation execution, and cleanup validation.

Technical Details

Testing tools and configuration:
  • JUnit Jupiter for test execution
  • Hutool Core for resource and ID utilities
  • Local file system configuration with base path and domain
  • Test resources in ‘file/erweima.jpg’
  • Content type specification for image uploads

Best Practices Demonstrated

The test implementation showcases several testing best practices for file handling operations.

Notable practices include:
  • Proper resource cleanup after testing
  • Configuration isolation for testing environment
  • Use of unique identifiers for test files
  • Clear separation of setup, execution, and cleanup phases

yunaiv/ruoyi-vue-pro

yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/framework/file/core/local/LocalFileClientTest.java

            
package cn.iocoder.yudao.module.infra.framework.file.core.local;

import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.util.IdUtil;
import cn.iocoder.yudao.module.infra.framework.file.core.client.local.LocalFileClient;
import cn.iocoder.yudao.module.infra.framework.file.core.client.local.LocalFileClientConfig;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

public class LocalFileClientTest {

    @Test
    @Disabled
    public void test() {
        // 创建客户端
        LocalFileClientConfig config = new LocalFileClientConfig();
        config.setDomain("http://127.0.0.1:48080");
        config.setBasePath("/Users/yunai/file_test");
        LocalFileClient client = new LocalFileClient(0L, config);
        client.init();
        // 上传文件
        String path = IdUtil.fastSimpleUUID() + ".jpg";
        byte[] content = ResourceUtil.readBytes("file/erweima.jpg");
        String fullPath = client.upload(content, path, "image/jpeg");
        System.out.println("访问地址:" + fullPath);
        client.delete(path);
    }

}