单元测试--

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);
        }
    }

}
相关推荐
菠萝猫yena2 天前
【读书笔记】《测试架构师修炼之道》读书笔记
功能测试·测试工具·单元测试
慧一居士2 天前
冒烟自测用例怎么写?
功能测试·单元测试·测试用例·可用性测试·模块测试
前端若水3 天前
智能体测试策略:单元测试、集成测试与模拟LLM
单元测试·集成测试
小羊Yveesss3 天前
AI智能单元测试:覆盖率泡沫与可信测试的产业破局
人工智能·单元测试
测试员周周3 天前
【AI测试路线图2】功能测试转 AI 测试:4~5 个月,一条最稳的路
开发语言·人工智能·python·功能测试·测试工具·单元测试·pytest
川石课堂软件测试3 天前
接口测试常见面试题及答案
python·网络协议·mysql·华为·单元测试·prometheus·harmonyos
MC皮蛋侠客3 天前
Catch2 单元测试指南
单元测试·catch2
诸葛李3 天前
集成构建xxxxx
java·junit·单元测试
yeshan4 天前
【Draft】基于 cluacov 的 Lua 代码分支覆盖率统计:从行级近似到指令级精确
单元测试·lua