单元测试--

mvn 命令:mvn clean verify -U -Dcheckstyle.skip -Dmaven.gitcommitid.skip=true

java 复制代码
import com.futures.busi.proxy.common.service.facade.response.GetDownLoadResultResponse;
import com.futures.busi.proxy.core.service.impl.download.DownLoadServiceImpl;
import com.kucoin.base.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.when;

/**
 * @author : Timmy 2026/1/19 description :
 */
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
@Slf4j
public class DownLoadServiceImplTest {
    @InjectMocks
    private DownLoadServiceImpl downLoadServiceImpl;

    @Mock
    private StringRedisTemplate stringRedisTemplate;

    @Test
    void getDownLoadResultTest() {
        // 参数为空
        {
            assertThrows(BizException.class, () -> downLoadServiceImpl.getDownLoadResult(null));
        }

        // 如果状态是成功的,但是下载链接为空,则尝试再次获取  如果依然为空说明下载链接丢失
        {
            ValueOperations valueOperations = mock(ValueOperations.class);
            when(stringRedisTemplate.opsForValue()).thenReturn(valueOperations);
            when(valueOperations.get("busi_download_status_prefix_1")).thenReturn("2");
            when(valueOperations.get("busi_download_url_prefix_1")).thenReturn(null);
            GetDownLoadResultResponse downLoadResult = downLoadServiceImpl.getDownLoadResult("1");
            log.info("downLoadResult: {}", downLoadResult);
        }
        // 如果状态为空 但是下载链接非空 则说明文件生成成功
        {
            ValueOperations valueOperations = mock(ValueOperations.class);
            when(stringRedisTemplate.opsForValue()).thenReturn(valueOperations);
            when(valueOperations.get("busi_download_status_prefix_1")).thenReturn(null);
            when(valueOperations.get("busi_download_url_prefix_1")).thenReturn("http://test.com/download/1");
            GetDownLoadResultResponse downLoadResult = downLoadServiceImpl.getDownLoadResult("1");
            log.info("downLoadResult: {}", downLoadResult);
        }
    }

}
相关推荐
liulilittle16 小时前
Lua中检测32位序号环绕的方法
junit·单元测试·lua
汽车仪器仪表相关领域1 天前
液力传动精准标定 + 智能换挡控制,动力总成测试新高度:GZCVL T‑IV 变矩器变速箱测试系统实战全解
功能测试·单元测试·汽车·压力测试·可用性测试·安全性测试
程序员小远2 天前
单元测试知识详解
自动化测试·软件测试·python·测试工具·职场和发展·单元测试·测试用例
Warren982 天前
Spring Boot + JUnit5 + Allure 测试报告完整指南
java·spring boot·后端·面试·单元测试·集成测试·模块测试
steel80882 天前
Spring Boot 整合 log4j2 日志配置教程
spring boot·单元测试·log4j
川石课堂软件测试3 天前
接口测试需要注意的一些BUG
网络·数据库·python·单元测试·bug·压力测试·tornado
CodeCraft Studio3 天前
Parasoft C/C++嵌入式软件测试解决方案:安全、可靠且符合标准
开发语言·c++·安全·单元测试·代码规范·parasoft·嵌入式软件测试
汽车仪器仪表相关领域4 天前
全工况精准标定 + 智能安全防护,建筑机械防坠生命线:GZCVL T‑II 安全防坠器测试系统实战全解
功能测试·测试工具·安全·单元测试·汽车·压力测试·可用性测试
KevinGuo4574 天前
【前后端开发知识 - 边开发边学习】什么的单元测试、集成测试和E2E测试?
学习·单元测试·集成测试
南昌彭于晏4 天前
springcloud+openFeign单元测试解决初始化循环依赖的问题
spring·spring cloud·单元测试