业务代码
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);
/**
* @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("完毕");
}
}
标签:springboot,list,单元测试,mock,languageTableDO,import,haier,com,LanguageTableDO
From: https://blog.csdn.net/qq398581645/article/details/142649279