mock数据,不使用springboot的单元测试

业务代码

java 复制代码
package com.haier.configure.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haier.common.util.RequestUtil;
import com.haier.configure.entity.LanguageTableDO;
import com.haier.configure.mapper.LanguageTableMapper;
import com.haier.configure.service.LanguageTableService;
import com.haier.configure.util.CheckDataUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author A2001111
 * @date 2024/4/3010:24
 */
@Slf4j
@Service
public class LanguageTableServiceImpl extends ServiceImpl<LanguageTableMapper, LanguageTableDO> implements LanguageTableService {

    @Override
    public void test1() {
        System.out.println("业务1...");
        String appId = RequestUtil.getAppId();
        System.out.println("appid:"+appId);
        System.out.println("第一次调用-----");
        List<LanguageTableDO> list = this.list(Wrappers.lambdaQuery(LanguageTableDO.class).eq(LanguageTableDO::getId, 1));
        for (LanguageTableDO languageTableDO : list) {
            System.out.println("循环结果1:"+languageTableDO.getChineseName());
        }

        System.out.println("第二次调用-----");
        List<LanguageTableDO> list2 = this.list(Wrappers.lambdaQuery(LanguageTableDO.class).eq(LanguageTableDO::getId, 1));
        for (LanguageTableDO languageTableDO : list2) {
            System.out.println("循环结果2:"+languageTableDO.getChineseName());
        }

        System.out.println("第3次调用-----");
        List<LanguageTableDO> list3 = this.list(Wrappers.lambdaQuery(LanguageTableDO.class).eq(LanguageTableDO::getId, 1));
        for (LanguageTableDO languageTableDO : list3) {
            System.out.println("循环结果3:"+languageTableDO.getChineseName());
        }

        System.out.println("业务2...");
    }


}

需要mock静态方法RequestUtil.getAppId(),需要mock成员方法this.list(lambdaQuery);

java 复制代码
/**
 * @author A2001111
 * @date 2024/9/30 9:07
 */
package com.haier.configure.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.haier.common.util.RequestUtil;
import com.haier.configure.entity.LanguageTableDO;
import com.haier.configure.mapper.LanguageTableMapper;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.*;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;

import java.util.ArrayList;
import java.util.List;

import static org.mockito.Mockito.any;
import static org.mockito.Mockito.when;

@MockitoSettings(strictness = Strictness.LENIENT)
class LanguageTableServiceImplTest<M extends LanguageTableMapper> {
    @InjectMocks
    @Spy
    private LanguageTableServiceImpl languageTableServiceImpl;
    @Mock
    private M mapper;

    private AutoCloseable mockitoCloseable;

    @BeforeEach
    void setUp() throws Exception {
        mockitoCloseable = MockitoAnnotations.openMocks(this);
    }

    @AfterEach
    void tearDown() throws Exception {
        mockitoCloseable.close();
    }

    @Test
    void test_getAllList_should_return_not_null_when_condition() throws Exception {
        // mock静态
        MockedStatic<RequestUtil> requestUtilMockedStatic = Mockito.mockStatic(RequestUtil.class);
        requestUtilMockedStatic.when(RequestUtil::getAppId).thenReturn("假数据哦");
        // setup
        LanguageTableDO languageTableDO = new LanguageTableDO();
        languageTableDO.setId(111L);
        languageTableDO.setCode("code");
        languageTableDO.setEnglishName("englishName111");
        languageTableDO.setChineseName("chineseName111");
        List<LanguageTableDO> list = new ArrayList<>();
        list.add(languageTableDO);

        LanguageTableDO languageTableDO2 = new LanguageTableDO();
        languageTableDO2.setId(222L);
        languageTableDO2.setCode("code");
        languageTableDO2.setEnglishName("englishName222");
        languageTableDO2.setChineseName("chineseName222");
        List<LanguageTableDO> list2 = new ArrayList<>();
        list2.add(languageTableDO2);

        // when(mapper.selectList(any(LambdaQueryWrapper.class))).thenReturn(list);
        // 也可以设置多次调用返回不同的结果,比如:第一次返回list,第二次返回list2,后续多次调用,都返回list2
        when(mapper.selectList(any(LambdaQueryWrapper.class)))
                .thenReturn(list)
                .thenReturn(list2);
        // run the test
        languageTableServiceImpl.test1();
        System.out.println("完毕");
    }


}
相关推荐
uhakadotcom27 分钟前
OpenTelemetry入门:让你的应用程序更透明
后端·面试·github
橘猫云计算机设计35 分钟前
基于springboot的考研成绩查询系统(源码+lw+部署文档+讲解),源码可白嫖!
java·spring boot·后端·python·考研·django·毕业设计
有一只柴犬1 小时前
深入Spring AI:6大核心概念带你入门AI开发
spring boot·后端
Aurora_NeAr1 小时前
深入理解Java虚拟机-垃圾收集器与内存分配策略
后端
向阳2561 小时前
SpringBoot+vue前后端分离整合sa-token(无cookie登录态 & 详细的登录流程)
java·vue.js·spring boot·后端·sa-token·springboot·登录流程
你的人类朋友1 小时前
JS严格模式,启动!
javascript·后端·node.js
Aurora_NeAr1 小时前
深入理解Java虚拟机-Java内存区域与内存溢出异常
后端
XiaoLeisj1 小时前
【MyBatis】深入解析 MyBatis XML 开发:增删改查操作和方法命名规范、@Param 重命名参数、XML 返回自增主键方法
xml·java·数据库·spring boot·sql·intellij-idea·mybatis
风象南1 小时前
SpringBoot实现数据库读写分离的3种方案
java·spring boot·后端
lzj20141 小时前
DataPermissionInterceptor源码解读
后端