单元测试--

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

}
相关推荐
软件检测小牛玛1 天前
具备软件功能测试资质的机构哪家更权威?山东软件测评机构 中承信安
功能测试·单元测试·软件测试报告·软件测评机构
闻哥1 天前
从测试坏味道到优雅实践:打造高质量单元测试
java·面试·单元测试·log4j·springboot
Warren981 天前
Pytest Fixture 作用域与接口测试 Token 污染问题实战解析
功能测试·面试·单元测试·集成测试·pytest·postman·模块测试
知行合一。。。1 天前
程序中的log4j、stderr、stdout日志
python·单元测试·log4j
测试秃头怪2 天前
面试大厂就靠这份软件测试八股文了【含答案】
自动化测试·软件测试·python·功能测试·面试·职场和发展·单元测试
测试大圣2 天前
软件测试基础知识总结(超全的)
软件测试·python·功能测试·测试工具·职场和发展·单元测试·测试用例
CodeCraft Studio2 天前
【Parasoft案例分享】在 DO-178C 标准下,如何实现航空嵌入式软件测试自动化
单元测试·自动化·静态分析·代码覆盖率·parasoft·do-178c·软件自动化测试
懒羊羊大王&3 天前
软件测试之博客系统项目实战(补充和解析部分)
selenium·单元测试·测试用例·集成测试
真智AI3 天前
用 LLM 辅助生成可跑的 Python 单元测试:pytest + coverage 覆盖率报告(含运行指令与排坑)
python·单元测试·pytest