mirror of
				https://gitee.com/hhyykk/ipms-sjy.git
				synced 2025-11-01 02:38:43 +08:00 
			
		
		
		
	完善 mail 单元测试
This commit is contained in:
		| @@ -6,7 +6,6 @@ import cn.iocoder.yudao.module.system.controller.admin.mail.vo.account.MailAccou | ||||
| import cn.iocoder.yudao.module.system.controller.admin.mail.vo.account.MailAccountPageReqVO; | ||||
| import cn.iocoder.yudao.module.system.controller.admin.mail.vo.account.MailAccountUpdateReqVO; | ||||
| import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailAccountDO; | ||||
| import cn.iocoder.yudao.module.system.dal.dataobject.permission.RoleDO; | ||||
| import cn.iocoder.yudao.module.system.dal.mysql.mail.MailAccountMapper; | ||||
| import cn.iocoder.yudao.module.system.mq.producer.mail.MailProducer; | ||||
| import org.junit.jupiter.api.Test; | ||||
| @@ -14,7 +13,7 @@ import org.springframework.boot.test.mock.mockito.MockBean; | ||||
| import org.springframework.context.annotation.Import; | ||||
|  | ||||
| import javax.annotation.Resource; | ||||
|  | ||||
| import java.util.List; | ||||
| import java.util.Map; | ||||
|  | ||||
| import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId; | ||||
| @@ -23,7 +22,9 @@ import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServic | ||||
| import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*; | ||||
| import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.MAIL_ACCOUNT_NOT_EXISTS; | ||||
| import static org.junit.jupiter.api.Assertions.*; | ||||
| import static org.mockito.ArgumentMatchers.eq; | ||||
| import static org.mockito.Mockito.verify; | ||||
| import static org.mockito.Mockito.when; | ||||
|  | ||||
| /** | ||||
| * {@link MailAccountServiceImpl} 的单元测试类 | ||||
| @@ -109,6 +110,8 @@ public class MailAccountServiceImplTest extends BaseDbUnitTest { | ||||
|         mailAccountMapper.insert(dbMailAccount);// @Sql: 先插入出一条存在的数据 | ||||
|         // 准备参数 | ||||
|         Long id = dbMailAccount.getId(); | ||||
|         // mock 方法(无关联模版) | ||||
|         when(mailTemplateService.countByAccountId(eq(id))).thenReturn(0L); | ||||
|  | ||||
|         // 调用 | ||||
|         mailAccountService.deleteMailAccount(id); | ||||
| @@ -117,6 +120,21 @@ public class MailAccountServiceImplTest extends BaseDbUnitTest { | ||||
|         verify(mailProducer).sendMailAccountRefreshMessage(); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testGetMailAccountFromCache() { | ||||
|         // mock 数据 | ||||
|         MailAccountDO dbMailAccount = randomPojo(MailAccountDO.class); | ||||
|         mailAccountMapper.insert(dbMailAccount);// @Sql: 先插入出一条存在的数据 | ||||
|         mailAccountService.initLocalCache(); | ||||
|         // 准备参数 | ||||
|         Long id = dbMailAccount.getId(); | ||||
|  | ||||
|         // 调用 | ||||
|         MailAccountDO mailAccount = mailAccountService.getMailAccountFromCache(id); | ||||
|         // 断言 | ||||
|         assertPojoEquals(dbMailAccount, mailAccount); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testDeleteMailAccount_notExists() { | ||||
|         // 准备参数 | ||||
| @@ -151,4 +169,35 @@ public class MailAccountServiceImplTest extends BaseDbUnitTest { | ||||
|        assertPojoEquals(dbMailAccount, pageResult.getList().get(0)); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testGetMailAccount() { | ||||
|         // mock 数据 | ||||
|         MailAccountDO dbMailAccount = randomPojo(MailAccountDO.class); | ||||
|         mailAccountMapper.insert(dbMailAccount);// @Sql: 先插入出一条存在的数据 | ||||
|         // 准备参数 | ||||
|         Long id = dbMailAccount.getId(); | ||||
|  | ||||
|         // 调用 | ||||
|         MailAccountDO mailAccount = mailAccountService.getMailAccount(id); | ||||
|         // 断言 | ||||
|         assertPojoEquals(dbMailAccount, mailAccount); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testGetMailAccountList() { | ||||
|         // mock 数据 | ||||
|         MailAccountDO dbMailAccount01 = randomPojo(MailAccountDO.class); | ||||
|         mailAccountMapper.insert(dbMailAccount01); | ||||
|         MailAccountDO dbMailAccount02 = randomPojo(MailAccountDO.class); | ||||
|         mailAccountMapper.insert(dbMailAccount02); | ||||
|         // 准备参数 | ||||
|  | ||||
|         // 调用 | ||||
|         List<MailAccountDO> list = mailAccountService.getMailAccountList(); | ||||
|         // 断言 | ||||
|         assertEquals(2, list.size()); | ||||
|         assertPojoEquals(dbMailAccount01, list.get(0)); | ||||
|         assertPojoEquals(dbMailAccount02, list.get(1)); | ||||
|     } | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -116,6 +116,20 @@ public class MailLogServiceImplTest extends BaseDbUnitTest { | ||||
|         assertEquals("NullPointerException: 测试异常", dbLog.getSendException()); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testGetMailLog() { | ||||
|         // mock 数据 | ||||
|         MailLogDO dbMailLog = randomPojo(MailLogDO.class, o -> o.setTemplateParams(randomTemplateParams())); | ||||
|         mailLogMapper.insert(dbMailLog); | ||||
|         // 准备参数 | ||||
|         Long id = dbMailLog.getId(); | ||||
|  | ||||
|         // 调用 | ||||
|         MailLogDO mailLog = mailLogService.getMailLog(id); | ||||
|         // 断言 | ||||
|         assertPojoEquals(dbMailLog, mailLog); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testGetMailLogPage() { | ||||
|        // mock 数据 | ||||
|   | ||||
| @@ -6,14 +6,20 @@ import cn.hutool.extra.mail.MailUtil; | ||||
| import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; | ||||
| import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; | ||||
| import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest; | ||||
| import cn.iocoder.yudao.framework.test.core.util.RandomUtils; | ||||
| import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailAccountDO; | ||||
| import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailTemplateDO; | ||||
| import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO; | ||||
| import cn.iocoder.yudao.module.system.mq.message.mail.MailSendMessage; | ||||
| import cn.iocoder.yudao.module.system.mq.producer.mail.MailProducer; | ||||
| import cn.iocoder.yudao.module.system.service.member.MemberService; | ||||
| import cn.iocoder.yudao.module.system.service.user.AdminUserService; | ||||
| import org.assertj.core.util.Lists; | ||||
| import org.junit.jupiter.api.Disabled; | ||||
| import org.junit.jupiter.api.Test; | ||||
| import org.mockito.InjectMocks; | ||||
| import org.mockito.Mock; | ||||
| import org.mockito.MockedStatic; | ||||
|  | ||||
| import java.util.HashMap; | ||||
| import java.util.Map; | ||||
| @@ -23,6 +29,7 @@ import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServic | ||||
| import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*; | ||||
| import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; | ||||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||||
| import static org.mockito.ArgumentMatchers.eq; | ||||
| import static org.mockito.Mockito.*; | ||||
|  | ||||
| @@ -31,6 +38,10 @@ class MailSendServiceImplTest extends BaseMockitoUnitTest { | ||||
|     @InjectMocks | ||||
|     private MailSendServiceImpl mailSendService; | ||||
|  | ||||
|     @Mock | ||||
|     private AdminUserService adminUserService; | ||||
|     @Mock | ||||
|     private MemberService memberService; | ||||
|     @Mock | ||||
|     private MailAccountService mailAccountService; | ||||
|     @Mock | ||||
| @@ -55,6 +66,82 @@ class MailSendServiceImplTest extends BaseMockitoUnitTest { | ||||
|         System.out.println("发送结果:" + messageId); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testSendSingleMailToAdmin() { | ||||
|         // 准备参数 | ||||
|         Long userId = randomLongId(); | ||||
|         String templateCode = RandomUtils.randomString(); | ||||
|         Map<String, Object> templateParams = MapUtil.<String, Object>builder().put("code", "1234") | ||||
|                 .put("op", "login").build(); | ||||
|         // mock adminUserService 的方法 | ||||
|         AdminUserDO user = randomPojo(AdminUserDO.class, o -> o.setMobile("15601691300")); | ||||
|         when(adminUserService.getUser(eq(userId))).thenReturn(user); | ||||
|  | ||||
|         // mock MailTemplateService 的方法 | ||||
|         MailTemplateDO template = randomPojo(MailTemplateDO.class, o -> { | ||||
|             o.setStatus(CommonStatusEnum.ENABLE.getStatus()); | ||||
|             o.setContent("验证码为{code}, 操作为{op}"); | ||||
|             o.setParams(Lists.newArrayList("code", "op")); | ||||
|         }); | ||||
|         when(mailTemplateService.getMailTemplateByCodeFromCache(eq(templateCode))).thenReturn(template); | ||||
|         String content = RandomUtils.randomString(); | ||||
|         when(mailTemplateService.formatMailTemplateContent(eq(template.getContent()), eq(templateParams))) | ||||
|                 .thenReturn(content); | ||||
|         // mock MailAccountService 的方法 | ||||
|         MailAccountDO account = randomPojo(MailAccountDO.class); | ||||
|         when(mailAccountService.getMailAccountFromCache(eq(template.getAccountId()))).thenReturn(account); | ||||
|         // mock MailLogService 的方法 | ||||
|         Long mailLogId = randomLongId(); | ||||
|         when(mailLogService.createMailLog(eq(userId), eq(UserTypeEnum.ADMIN.getValue()), eq(user.getEmail()), | ||||
|                 eq(account), eq(template), eq(content), eq(templateParams), eq(true))).thenReturn(mailLogId); | ||||
|  | ||||
|         // 调用 | ||||
|         Long resultMailLogId = mailSendService.sendSingleMailToAdmin(null, userId, templateCode, templateParams); | ||||
|         // 断言 | ||||
|         assertEquals(mailLogId, resultMailLogId); | ||||
|         // 断言调用 | ||||
|         verify(mailProducer).sendMailSendMessage(eq(mailLogId), eq(user.getEmail()), | ||||
|                 eq(account.getId()), eq(template.getNickname()), eq(template.getTitle()), eq(content)); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testSendSingleMailToMember() { | ||||
|         // 准备参数 | ||||
|         Long userId = randomLongId(); | ||||
|         String templateCode = RandomUtils.randomString(); | ||||
|         Map<String, Object> templateParams = MapUtil.<String, Object>builder().put("code", "1234") | ||||
|                 .put("op", "login").build(); | ||||
|         // mock memberService 的方法 | ||||
|         String mail = randomEmail(); | ||||
|         when(memberService.getMemberUserEmail(eq(userId))).thenReturn(mail); | ||||
|  | ||||
|         // mock MailTemplateService 的方法 | ||||
|         MailTemplateDO template = randomPojo(MailTemplateDO.class, o -> { | ||||
|             o.setStatus(CommonStatusEnum.ENABLE.getStatus()); | ||||
|             o.setContent("验证码为{code}, 操作为{op}"); | ||||
|             o.setParams(Lists.newArrayList("code", "op")); | ||||
|         }); | ||||
|         when(mailTemplateService.getMailTemplateByCodeFromCache(eq(templateCode))).thenReturn(template); | ||||
|         String content = RandomUtils.randomString(); | ||||
|         when(mailTemplateService.formatMailTemplateContent(eq(template.getContent()), eq(templateParams))) | ||||
|                 .thenReturn(content); | ||||
|         // mock MailAccountService 的方法 | ||||
|         MailAccountDO account = randomPojo(MailAccountDO.class); | ||||
|         when(mailAccountService.getMailAccountFromCache(eq(template.getAccountId()))).thenReturn(account); | ||||
|         // mock MailLogService 的方法 | ||||
|         Long mailLogId = randomLongId(); | ||||
|         when(mailLogService.createMailLog(eq(userId), eq(UserTypeEnum.MEMBER.getValue()), eq(mail), | ||||
|                 eq(account), eq(template), eq(content), eq(templateParams), eq(true))).thenReturn(mailLogId); | ||||
|  | ||||
|         // 调用 | ||||
|         Long resultMailLogId = mailSendService.sendSingleMailToMember(null, userId, templateCode, templateParams); | ||||
|         // 断言 | ||||
|         assertEquals(mailLogId, resultMailLogId); | ||||
|         // 断言调用 | ||||
|         verify(mailProducer).sendMailSendMessage(eq(mailLogId), eq(mail), | ||||
|                 eq(account.getId()), eq(template.getNickname()), eq(template.getTitle()), eq(content)); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 发送成功,当短信模板开启时 | ||||
|      */ | ||||
| @@ -64,7 +151,7 @@ class MailSendServiceImplTest extends BaseMockitoUnitTest { | ||||
|         String mail = randomEmail(); | ||||
|         Long userId = randomLongId(); | ||||
|         Integer userType = randomEle(UserTypeEnum.values()).getValue(); | ||||
|         String templateCode = randomString(); | ||||
|         String templateCode = RandomUtils.randomString(); | ||||
|         Map<String, Object> templateParams = MapUtil.<String, Object>builder().put("code", "1234") | ||||
|                 .put("op", "login").build(); | ||||
|         // mock MailTemplateService 的方法 | ||||
| @@ -74,7 +161,7 @@ class MailSendServiceImplTest extends BaseMockitoUnitTest { | ||||
|             o.setParams(Lists.newArrayList("code", "op")); | ||||
|         }); | ||||
|         when(mailTemplateService.getMailTemplateByCodeFromCache(eq(templateCode))).thenReturn(template); | ||||
|         String content = randomString(); | ||||
|         String content = RandomUtils.randomString(); | ||||
|         when(mailTemplateService.formatMailTemplateContent(eq(template.getContent()), eq(templateParams))) | ||||
|                 .thenReturn(content); | ||||
|         // mock MailAccountService 的方法 | ||||
| @@ -103,7 +190,7 @@ class MailSendServiceImplTest extends BaseMockitoUnitTest { | ||||
|         String mail = randomEmail(); | ||||
|         Long userId = randomLongId(); | ||||
|         Integer userType = randomEle(UserTypeEnum.values()).getValue(); | ||||
|         String templateCode = randomString(); | ||||
|         String templateCode = RandomUtils.randomString(); | ||||
|         Map<String, Object> templateParams = MapUtil.<String, Object>builder().put("code", "1234") | ||||
|                 .put("op", "login").build(); | ||||
|         // mock MailTemplateService 的方法 | ||||
| @@ -113,7 +200,7 @@ class MailSendServiceImplTest extends BaseMockitoUnitTest { | ||||
|             o.setParams(Lists.newArrayList("code", "op")); | ||||
|         }); | ||||
|         when(mailTemplateService.getMailTemplateByCodeFromCache(eq(templateCode))).thenReturn(template); | ||||
|         String content = randomString(); | ||||
|         String content = RandomUtils.randomString(); | ||||
|         when(mailTemplateService.formatMailTemplateContent(eq(template.getContent()), eq(templateParams))) | ||||
|                 .thenReturn(content); | ||||
|         // mock MailAccountService 的方法 | ||||
| @@ -134,18 +221,18 @@ class MailSendServiceImplTest extends BaseMockitoUnitTest { | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testCheckMailTemplateValid_notExists() { | ||||
|     public void testValidateMailTemplateValid_notExists() { | ||||
|         // 准备参数 | ||||
|         String templateCode = randomString(); | ||||
|         String templateCode = RandomUtils.randomString(); | ||||
|         // mock 方法 | ||||
|  | ||||
|         // 调用,并断言异常 | ||||
|         assertServiceException(() -> mailSendService.checkMailTemplateValid(templateCode), | ||||
|         assertServiceException(() -> mailSendService.validateMailTemplate(templateCode), | ||||
|                 MAIL_TEMPLATE_NOT_EXISTS); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testCheckTemplateParams_paramMiss() { | ||||
|     public void testValidateTemplateParams_paramMiss() { | ||||
|         // 准备参数 | ||||
|         MailTemplateDO template = randomPojo(MailTemplateDO.class, | ||||
|                 o -> o.setParams(Lists.newArrayList("code"))); | ||||
| @@ -153,18 +240,80 @@ class MailSendServiceImplTest extends BaseMockitoUnitTest { | ||||
|         // mock 方法 | ||||
|  | ||||
|         // 调用,并断言异常 | ||||
|         assertServiceException(() -> mailSendService.checkTemplateParams(template, templateParams), | ||||
|         assertServiceException(() -> mailSendService.validateTemplateParams(template, templateParams), | ||||
|                 MAIL_SEND_TEMPLATE_PARAM_MISS, "code"); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testCheckMail_notExists() { | ||||
|     public void testValidateMail_notExists() { | ||||
|         // 准备参数 | ||||
|         // mock 方法 | ||||
|  | ||||
|         // 调用,并断言异常 | ||||
|         assertServiceException(() -> mailSendService.checkMail(null), | ||||
|         assertServiceException(() -> mailSendService.validateMail(null), | ||||
|                 MAIL_SEND_MAIL_NOT_EXISTS); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testDoSendMail_success() { | ||||
|         try (MockedStatic<MailUtil> mailUtilMock = mockStatic(MailUtil.class)) { | ||||
|             // 准备参数 | ||||
|             MailSendMessage message = randomPojo(MailSendMessage.class, o -> o.setNickname("芋艿")); | ||||
|             // mock 方法(获得邮箱账号) | ||||
|             MailAccountDO account = randomPojo(MailAccountDO.class, o -> o.setMail("7685@qq.com")); | ||||
|             when(mailAccountService.getMailAccountFromCache(eq(message.getAccountId()))) | ||||
|                     .thenReturn(account); | ||||
|  | ||||
|             // mock 方法(发送邮件) | ||||
|             String messageId = randomString(); | ||||
|             mailUtilMock.when(() -> MailUtil.send(argThat(mailAccount -> { | ||||
|                 assertEquals("芋艿 <7685@qq.com>", mailAccount.getFrom()); | ||||
|                 assertTrue(mailAccount.isAuth()); | ||||
|                 assertEquals(account.getUsername(), mailAccount.getUser()); | ||||
|                 assertEquals(account.getPassword(), mailAccount.getPass()); | ||||
|                 assertEquals(account.getHost(), mailAccount.getHost()); | ||||
|                 assertEquals(account.getPort(), mailAccount.getPort()); | ||||
|                 assertEquals(account.getSslEnable(), mailAccount.isSslEnable()); | ||||
|                 return true; | ||||
|             }), eq(message.getMail()), eq(message.getTitle()), eq(message.getContent()), eq(true))) | ||||
|                     .thenReturn(messageId); | ||||
|  | ||||
|             // 调用 | ||||
|             mailSendService.doSendMail(message); | ||||
|             // 断言 | ||||
|             verify(mailLogService).updateMailSendResult(eq(message.getLogId()), eq(messageId), isNull()); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testDoSendMail_exception() { | ||||
|         try (MockedStatic<MailUtil> mailUtilMock = mockStatic(MailUtil.class)) { | ||||
|             // 准备参数 | ||||
|             MailSendMessage message = randomPojo(MailSendMessage.class, o -> o.setNickname("芋艿")); | ||||
|             // mock 方法(获得邮箱账号) | ||||
|             MailAccountDO account = randomPojo(MailAccountDO.class, o -> o.setMail("7685@qq.com")); | ||||
|             when(mailAccountService.getMailAccountFromCache(eq(message.getAccountId()))) | ||||
|                     .thenReturn(account); | ||||
|  | ||||
|             // mock 方法(发送邮件) | ||||
|             Exception e = new NullPointerException("啦啦啦"); | ||||
|             mailUtilMock.when(() -> MailUtil.send(argThat(mailAccount -> { | ||||
|                         assertEquals("芋艿 <7685@qq.com>", mailAccount.getFrom()); | ||||
|                         assertTrue(mailAccount.isAuth()); | ||||
|                         assertEquals(account.getUsername(), mailAccount.getUser()); | ||||
|                         assertEquals(account.getPassword(), mailAccount.getPass()); | ||||
|                         assertEquals(account.getHost(), mailAccount.getHost()); | ||||
|                         assertEquals(account.getPort(), mailAccount.getPort()); | ||||
|                         assertEquals(account.getSslEnable(), mailAccount.isSslEnable()); | ||||
|                         return true; | ||||
|                     }), eq(message.getMail()), eq(message.getTitle()), eq(message.getContent()), eq(true))) | ||||
|                     .thenThrow(e); | ||||
|  | ||||
|             // 调用 | ||||
|             mailSendService.doSendMail(message); | ||||
|             // 断言 | ||||
|             verify(mailLogService).updateMailSendResult(eq(message.getLogId()), isNull(), same(e)); | ||||
|         } | ||||
|     } | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -6,7 +6,6 @@ import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; | ||||
| import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplateCreateReqVO; | ||||
| import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplatePageReqVO; | ||||
| import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplateUpdateReqVO; | ||||
| import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailAccountDO; | ||||
| import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailTemplateDO; | ||||
| import cn.iocoder.yudao.module.system.dal.mysql.mail.MailTemplateMapper; | ||||
| import cn.iocoder.yudao.module.system.mq.producer.mail.MailProducer; | ||||
| @@ -15,7 +14,8 @@ import org.springframework.boot.test.mock.mockito.MockBean; | ||||
| import org.springframework.context.annotation.Import; | ||||
|  | ||||
| import javax.annotation.Resource; | ||||
|  | ||||
| import java.util.HashMap; | ||||
| import java.util.List; | ||||
| import java.util.Map; | ||||
|  | ||||
| import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime; | ||||
| @@ -27,6 +27,7 @@ import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId | ||||
| import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo; | ||||
| import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.MAIL_TEMPLATE_NOT_EXISTS; | ||||
| import static org.junit.jupiter.api.Assertions.*; | ||||
| import static org.mockito.Mockito.verify; | ||||
|  | ||||
| /** | ||||
| * {@link MailTemplateServiceImpl} 的单元测试类 | ||||
| @@ -72,6 +73,7 @@ public class MailTemplateServiceImplTest extends BaseDbUnitTest { | ||||
|         // 校验记录的属性是否正确 | ||||
|         MailTemplateDO mailTemplate = mailTemplateMapper.selectById(mailTemplateId); | ||||
|         assertPojoEquals(reqVO, mailTemplate); | ||||
|         verify(mailProducer).sendMailTemplateRefreshMessage(); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
| @@ -89,6 +91,7 @@ public class MailTemplateServiceImplTest extends BaseDbUnitTest { | ||||
|         // 校验是否更新正确 | ||||
|         MailTemplateDO mailTemplate = mailTemplateMapper.selectById(reqVO.getId()); // 获取最新的 | ||||
|         assertPojoEquals(reqVO, mailTemplate); | ||||
|         verify(mailProducer).sendMailTemplateRefreshMessage(); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
| @@ -110,8 +113,9 @@ public class MailTemplateServiceImplTest extends BaseDbUnitTest { | ||||
|  | ||||
|         // 调用 | ||||
|         mailTemplateService.deleteMailTemplate(id); | ||||
|        // 校验数据不存在了 | ||||
|        assertNull(mailTemplateMapper.selectById(id)); | ||||
|         // 校验数据不存在了 | ||||
|         assertNull(mailTemplateMapper.selectById(id)); | ||||
|         verify(mailProducer).sendMailTemplateRefreshMessage(); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
| @@ -160,4 +164,77 @@ public class MailTemplateServiceImplTest extends BaseDbUnitTest { | ||||
|        assertPojoEquals(dbMailTemplate, pageResult.getList().get(0)); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testGetMailTemplateList() { | ||||
|         // mock 数据 | ||||
|         MailTemplateDO dbMailTemplate01 = randomPojo(MailTemplateDO.class); | ||||
|         mailTemplateMapper.insert(dbMailTemplate01); | ||||
|         MailTemplateDO dbMailTemplate02 = randomPojo(MailTemplateDO.class); | ||||
|         mailTemplateMapper.insert(dbMailTemplate02); | ||||
|  | ||||
|         // 调用 | ||||
|         List<MailTemplateDO> list = mailTemplateService.getMailTemplateList(); | ||||
|         // 断言 | ||||
|         assertEquals(2, list.size()); | ||||
|         assertEquals(dbMailTemplate01, list.get(0)); | ||||
|         assertEquals(dbMailTemplate02, list.get(1)); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testGetTemplate() { | ||||
|         // mock 数据 | ||||
|         MailTemplateDO dbMailTemplate = randomPojo(MailTemplateDO.class); | ||||
|         mailTemplateMapper.insert(dbMailTemplate); | ||||
|         // 准备参数 | ||||
|         Long id = dbMailTemplate.getId(); | ||||
|  | ||||
|         // 调用 | ||||
|         MailTemplateDO mailTemplate = mailTemplateService.getMailTemplate(id); | ||||
|         // 断言 | ||||
|         assertPojoEquals(dbMailTemplate, mailTemplate); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testGetMailTemplateByCodeFromCache() { | ||||
|         // mock 数据 | ||||
|         MailTemplateDO dbMailTemplate = randomPojo(MailTemplateDO.class); | ||||
|         mailTemplateMapper.insert(dbMailTemplate); | ||||
|         mailTemplateService.initLocalCache(); | ||||
|         // 准备参数 | ||||
|         String code = dbMailTemplate.getCode(); | ||||
|  | ||||
|         // 调用 | ||||
|         MailTemplateDO mailTemplate = mailTemplateService.getMailTemplateByCodeFromCache(code); | ||||
|         // 断言 | ||||
|         assertPojoEquals(dbMailTemplate, mailTemplate); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testFormatMailTemplateContent() { | ||||
|         // 准备参数 | ||||
|         Map<String, Object> params = new HashMap<>(); | ||||
|         params.put("name", "小红"); | ||||
|         params.put("what", "饭"); | ||||
|  | ||||
|         // 调用,并断言 | ||||
|         assertEquals("小红,你好,饭吃了吗?", | ||||
|                 mailTemplateService.formatMailTemplateContent("{name},你好,{what}吃了吗?", params)); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void testCountByAccountId() { | ||||
|         // mock 数据 | ||||
|         MailTemplateDO dbMailTemplate = randomPojo(MailTemplateDO.class); | ||||
|         mailTemplateMapper.insert(dbMailTemplate); | ||||
|         // 测试 accountId 不匹配 | ||||
|         mailTemplateMapper.insert(cloneIgnoreId(dbMailTemplate, o -> o.setAccountId(2L))); | ||||
|         // 准备参数 | ||||
|         Long accountId = dbMailTemplate.getAccountId(); | ||||
|  | ||||
|         // 调用 | ||||
|         long count = mailTemplateService.countByAccountId(accountId); | ||||
|         // 断言 | ||||
|         assertEquals(1, count); | ||||
|     } | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -101,7 +101,7 @@ public class SmsSendServiceImplTest extends BaseMockitoUnitTest { | ||||
|         String templateCode = randomString(); | ||||
|         Map<String, Object> templateParams = MapUtil.<String, Object>builder().put("code", "1234") | ||||
|                 .put("op", "login").build(); | ||||
|         // mock adminUserService 的方法 | ||||
|         // mock memberService 的方法 | ||||
|         String mobile = "15601691300"; | ||||
|         when(memberService.getMemberUserMobile(eq(userId))).thenReturn(mobile); | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 YunaiV
					YunaiV