一个Mockito的Demo
立即下载
资源介绍:
一个Mockito的Demo
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));
}
}