package com.mockitoTest;
import com.mockitoTest.entity.PeopleInfoDto;
import com.mockitoTest.mapper.PeopleInfoMapper;
import com.mockitoTest.service.PeopleInfoService;
import com.mockitoTest.service.impl.PeopleInfoServiceImpl;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Arrays;
/**
* @Author: Wulc
* @CreateTime: 2024-09-15
* @Description: 单元测试
* @Version: 1.0
*/
class PeopleInfoServiceMockitoTest {
//Mockito测试类不能是pubilc
//InjectMocks会调用实际的方法(InjectMocks只能修饰具体的class,不能修饰接口)
@InjectMocks
private PeopleInfoServiceImpl peopleInfoService;
@Mock
private PeopleInfoServiceImpl peopleInfoServiceMock;
//Mock不会调用实际方法,因此需设定其返回值。Mockito.when(调用的方法).thenReturn(你给定的返回值)
@Mock
private PeopleInfoMapper peopleInfoMapperMock;
@Autowired
private PeopleInfoService peopleInfoServiceAutowired;
@BeforeEach
void initBean() {
//让注解生效
MockitoAnnotations.initMocks(this);
}
@Test
void registerPerson() {
PeopleInfoDto peopleInfoDto = new PeopleInfoDto("qianqi", "钱七", "男", "420606198510233062", "18006588532", "18006588532@163.com", "www!@qw123456");
PeopleInfoDto peopleInfoDto1 = new PeopleInfoDto("error", "钱七", "男", "420606198510233062", "18006588532", "18006588532@163.com", "www!@qw123456");
//因为不想涉及实际数据库,所以所有PeopleInfoMapper类的方法,都自定一个调用此方法的返回值。
//这样在测试过程中如果有调用到PeopleInfoMapper类中的方法,就不会调用实际方法了,而是调用一个模拟方法,不会操作数据库。
Mockito.when(peopleInfoMapperMock.listAllPeopleId()).thenReturn(Arrays.asList("zhangsan12", "wangwu34"));
Mockito.when(peopleInfoMapperMock.addPeopleInfo(peopleInfoDto)).thenReturn(1);
Mockito.when(peopleInfoMapperMock.addPeopleInfo(peopleInfoDto1)).thenReturn(0);
//校验peopleId为null
PeopleInfoDto p0 = new PeopleInfoDto(null, "张三", "男", "420606198510233062", "15580703373", "15580703373@163.com", "www!@qw123456");
Assertions.assertEquals("PeopleId不能为null", peopleInfoService.registerPerson(p0));
//校验peopleId是否已存在
PeopleInfoDto p1 = new PeopleInfoDto("zhangsan12", "张三", "男", "420606198510233062", "15580703373", "15580703373@163.com", "www!@qw123456");
Assertions.assertEquals("PeopleId已经存在了", peopleInfoService.registerPerson(p1));
//校验身份证号是否合法
PeopleInfoDto p2 = new PeopleInfoDto("zhaoliu", "赵六", "男", "310107sasa196901033214", "13822297249", "13822297249@163.com", "www!@qw123456");
Assertions.assertEquals("身份证号不合法", peopleInfoService.registerPerson(p2));
//校验身份证号不能为null
p2.setIdCardNo(null);
Assertions.assertEquals("身份证号不能为null", peopleInfoService.registerPerson(p2));
//校验手机号是否合法
PeopleInfoDto p3 = new PeopleInfoDto("zhaoliu", "赵六", "男", "420606198510233062", "138222rr97249", "13822297249@163.com", "www!@qw123456");
Assertions.assertEquals("手机号不合法", peopleInfoService.registerPerson(p3));
//校验手机号不能为null
p3.setPhone(null);
Assertions.assertEquals("手机号不能为null", peopleInfoService.registerPerson(p3));
//校验邮箱是否合法
PeopleInfoDto p4 = new PeopleInfoDto("zhaoliu", "赵六", "男", "420606198510233062", "13822297249", "13822297249@##16323.com", "www!@qw123456");
Assertions.assertEquals("邮箱不合法", peopleInfoService.registerPerson(p4));
//校验邮箱不能为null
p4.setEmail(null);
Assertions.assertEquals("邮箱不能为null", peopleInfoService.registerPerson(p4));
//校验密码为null
PeopleInfoDto p5 = new PeopleInfoDto("zhaoliu", "赵六", "男", "420606198510233062", "13822297249", "13822297249@163.com", null);
Assertions.assertEquals("密码不能为null", peopleInfoService.registerPerson(p5));
//全部条件通过
Assertions.assertEquals("注册成功", peopleInfoService.registerPerson(peopleInfoDto));
//未知错误
Assertions.assertEquals("未知错误", peopleInfoService.registerPerson(peopleInfoDto1));
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
一个Mockito的Demo
共12个文件
java:7个
xml:2个
yml:1个
需积分: 0 1 下载量 135 浏览量
2024-09-16
15:30:49
上传
评论
收藏 12KB ZIP 举报
温馨提示
一个Mockito的Demo
资源推荐
资源详情
资源评论
收起资源包目录
MockitoTest.zip (12个子文件)
MockitoTest
pom.xml 4KB
src
test
java
com
mockitoTest
PeopleInfoServiceTest.java 1KB
PeopleInfoServiceMockitoTest.java 5KB
main
resources
mapper
PeopleInfoMapper.xml 759B
application.yml 241B
java
com
mockitoTest
SpringbootApplication.java 496B
mapper
PeopleInfoMapper.java 300B
service
impl
PeopleInfoServiceImpl.java 3KB
PeopleInfoService.java 191B
entity
PeopleInfoDto.java 490B
.gitignore 490B
MockitoTest.iml 210B
共 12 条
- 1
资源评论
金斗潼关
- 粉丝: 164
- 资源: 9
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- new_bird_c-c语言入门
- christmasTree-圣诞树html网页代码
- working-shell脚本入门——流程控制
- hadoop_install-sqoop数据导入
- ThinkCMF-mysql安装
- BigData-Notes-sqoop的安装与配置
- C语言-leetcode题解之28-implement-strstr.c
- C语言-leetcode题解之27-remove-element.c
- C语言-leetcode题解之26-remove-duplicates-from-sorted-array.c
- C语言-leetcode题解之24-swap-nodes-in-pairs.c
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功