test(sms): 添加生日短信发送功能的单元测试- 创建 BirthdaySmsServiceTest 测试类- 配置测试环境使用 dev profile
- 实现测试方法 testSendBirthdaySms 用于验证短信发送逻辑 - 添加测试专家数据构建与模板替换逻辑 - 集成 SmsHelper 进行实际短信发送测试 - 增加异常处理和测试结果输出日志master
parent
ffb4382070
commit
e550137ebf
|
|
@ -87,9 +87,9 @@ public class SmsBirthdayServiceImpl extends ServiceImpl<SmsBirthdayMapper, SmsBi
|
|||
|
||||
// 逐个发送个性化短信
|
||||
for (Specialist specialist : specialists) {
|
||||
String content = template.replace("{姓名}", specialist.getName());
|
||||
try {
|
||||
// 替换模板中的占位符
|
||||
String content = template.replace("{姓名}", specialist.getName());
|
||||
|
||||
// 创建短信日志记录
|
||||
SmsLog smsLog = new SmsLog();
|
||||
|
|
@ -122,8 +122,7 @@ public class SmsBirthdayServiceImpl extends ServiceImpl<SmsBirthdayMapper, SmsBi
|
|||
SmsLog failedSmsLog = new SmsLog();
|
||||
failedSmsLog.setName(specialist.getName())
|
||||
.setPhone(specialist.getPhone())
|
||||
.setContent(template.replace("{name}", specialist.getName())
|
||||
.replace("{title}", specialist.getAddress() != null ? specialist.getAddress() : ""))
|
||||
.setContent(content)
|
||||
.setRemark("生日短信-发送异常: " + e.getMessage())
|
||||
.setSendTm(new java.util.Date());
|
||||
smsLogMapper.insert(failedSmsLog);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,85 @@
|
|||
package com.whdc.service.impl;
|
||||
|
||||
import com.whdc.model.entity.Specialist;
|
||||
import com.whdc.utils.SmsHelper;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 生日短信服务测试类 - 使用dev profile
|
||||
*
|
||||
* @author lyf
|
||||
* @since 2025-09-25
|
||||
*/
|
||||
@SpringBootTest(classes = com.whdc.FxkhTxlApiApplication.class)
|
||||
@ActiveProfiles("dev")
|
||||
public class BirthdaySmsServiceTest {
|
||||
|
||||
@Autowired
|
||||
private SmsHelper smsHelper;
|
||||
|
||||
@Test
|
||||
public void testSendBirthdaySms() {
|
||||
System.out.println("=== 开始测试生日短信发送功能 ===");
|
||||
|
||||
// 创建测试专家数据
|
||||
Specialist specialist = new Specialist();
|
||||
specialist.setName("李");
|
||||
specialist.setPhone("15671545233");
|
||||
specialist.setStatus(1);
|
||||
|
||||
List<Specialist> specialists = Arrays.asList(specialist);
|
||||
|
||||
System.out.println("专家信息:");
|
||||
System.out.println("姓名:" + specialist.getName());
|
||||
System.out.println("电话:" + specialist.getPhone());
|
||||
System.out.println("模板:{姓名},测试短信接口");
|
||||
|
||||
try {
|
||||
// 直接使用你提供的模板内容测试发送逻辑
|
||||
String template = "{姓名},测试短信接口";
|
||||
System.out.println("使用模板:" + template);
|
||||
|
||||
// 调用内部方法进行测试
|
||||
testSendWithTemplate(specialists, template);
|
||||
|
||||
System.out.println("=== 生日短信发送测试完成!===");
|
||||
} catch (Exception e) {
|
||||
System.err.println("测试过程中发生异常:" + e.getMessage());
|
||||
e.printStackTrace();
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用指定模板测试发送短信
|
||||
*/
|
||||
private void testSendWithTemplate(List<Specialist> specialists, String template) {
|
||||
System.out.println("开始使用模板发送短信...");
|
||||
|
||||
for (Specialist specialist : specialists) {
|
||||
try {
|
||||
// 替换模板中的占位符
|
||||
String content = template.replace("{姓名}", specialist.getName());
|
||||
System.out.println("生成的短信内容:" + content);
|
||||
|
||||
// 使用SmsHelper发送个性化短信
|
||||
List<String> phoneList = Collections.singletonList(specialist.getPhone());
|
||||
String sendResult = smsHelper.send(phoneList, content);
|
||||
|
||||
System.out.println("向专家" + specialist.getName() + "发送生日短信结果: " + sendResult);
|
||||
System.out.println("短信发送完成,结果:" + sendResult);
|
||||
|
||||
} catch (Exception e) {
|
||||
System.err.println("向专家" + specialist.getName() + "发送生日短信时发生异常: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue