Class InMemoryEmailSender
java.lang.Object
cloud.opencode.base.email.testing.InMemoryEmailSender
In-Memory Email Sender for Testing
内存邮件发送器(用于测试)
Captures all sent emails in memory without actually sending them. Useful for unit and integration tests.
在内存中捕获所有发送的邮件而不实际发送。适用于单元和集成测试。
Features | 主要功能:
- Capture sent emails in memory - 在内存中捕获发送的邮件
- Query by recipient, subject, content - 按收件人、主题、内容查询
- Thread-safe operations - 线程安全操作
- Failure simulation support - 模拟发送失败支持
Usage Examples | 使用示例:
InMemoryEmailSender sender = new InMemoryEmailSender();
OpenEmail.configure(config, sender);
OpenEmail.sendText("user@example.com", "Test", "Hello");
assertThat(sender.getSentCount()).isEqualTo(1);
assertThat(sender.getLastEmail().subject()).isEqualTo("Test");
assertThat(sender.findByRecipient("user@example.com")).hasSize(1);
sender.clear();
Security | 安全性:
- Thread-safe: Yes (CopyOnWriteArrayList) - 线程安全: 是
- Since:
- JDK 25, opencode-base-email V1.0.3
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Clear all captured emails 清空所有已捕获的邮件voidRemove failure simulator 移除失败模拟器default voidclose()Close the sender and release resources 关闭发送器并释放资源Find emails matching a predicate 查找匹配谓词的邮件findByRecipient(String recipient) Find emails sent to a specific recipient 查找发送到特定收件人的邮件findBySubject(String subjectPart) Find emails by subject (contains match) 按主题查找邮件(包含匹配)Get the last sent email 获取最后一封已发送邮件intGet the number of sent emails 获取已发送邮件数量Get all sent emails 获取所有已发送邮件booleanCheck if any email was sent to the recipient 检查是否有邮件发送到该收件人voidsendWithResult(Email email) voidsimulateFailure(Predicate<Email> failureSimulator) Set failure simulator - emails matching the predicate will throw an exception 设置失败模拟器 - 匹配谓词的邮件将抛出异常
-
Constructor Details
-
InMemoryEmailSender
public InMemoryEmailSender()
-
-
Method Details
-
send
-
sendWithResult
-
getSentEmails
-
getLastEmail
Get the last sent email 获取最后一封已发送邮件- Returns:
- the last email or null if none sent | 最后一封邮件,未发送则返回null
-
getSentCount
public int getSentCount()Get the number of sent emails 获取已发送邮件数量- Returns:
- the count | 数量
-
findByRecipient
-
findBySubject
-
findBy
-
hasSentTo
Check if any email was sent to the recipient 检查是否有邮件发送到该收件人- Parameters:
recipient- the recipient email address | 收件人邮箱地址- Returns:
- true if at least one email was sent | 至少发送一封返回true
-
clear
public void clear()Clear all captured emails 清空所有已捕获的邮件 -
simulateFailure
-
clearFailureSimulator
public void clearFailureSimulator()Remove failure simulator 移除失败模拟器 -
close
default void close()Close the sender and release resources 关闭发送器并释放资源
-