单元测试--

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

}
相关推荐
兰.lan1 天前
【黑马ai测试】黑马头条登录功能测试-发布功能测试-其他功能模块设计
软件测试·人工智能·笔记·python·功能测试·ai·单元测试
淼淼7631 天前
Qt单元测试
单元测试
Parasoft中国2 天前
单元测试告别人工!PARASOFT步入AI智能体时代!
单元测试
还是大剑师兰特2 天前
Vitest单元测试教程
单元测试
DJ斯特拉4 天前
Redis使用lua脚本
junit·单元测试·lua
念越4 天前
蓝桥杯单元测试模拟1期模拟题答案及代码解析
蓝桥杯·单元测试·测试
AIminminHu5 天前
OpenGL渲染与几何内核那点事-项目实践理论补充(三-1-(2):当你的CAD代码变得“又大又乱”:从手动编译到CMake,从随性编码到单元测试))
c++·单元测试·cmake·cad·cad开发
DolphinDB智臾科技6 天前
DolphinDB 单元测试教程
单元测试
春日见6 天前
TEST文件夹:Pytest,集成测试,单元测试
服务器·人工智能·驱动开发·单元测试·计算机外设·集成测试·pytest