Class InMemoryEmailSender

java.lang.Object
cloud.opencode.base.email.testing.InMemoryEmailSender

public class InMemoryEmailSender extends Object
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 Details

    • InMemoryEmailSender

      public InMemoryEmailSender()
  • Method Details

    • send

      public void send(Email email)
    • sendWithResult

      public SendResult sendWithResult(Email email)
    • getSentEmails

      public List<Email> getSentEmails()
      Get all sent emails 获取所有已发送邮件
      Returns:
      unmodifiable list of sent emails | 不可修改的已发送邮件列表
    • getLastEmail

      public Email 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

      public List<Email> findByRecipient(String recipient)
      Find emails sent to a specific recipient 查找发送到特定收件人的邮件
      Parameters:
      recipient - the recipient email address | 收件人邮箱地址
      Returns:
      matching emails | 匹配的邮件列表
    • findBySubject

      public List<Email> findBySubject(String subjectPart)
      Find emails by subject (contains match) 按主题查找邮件(包含匹配)
      Parameters:
      subjectPart - the subject part to match | 要匹配的主题部分
      Returns:
      matching emails | 匹配的邮件列表
    • findBy

      public List<Email> findBy(Predicate<Email> predicate)
      Find emails matching a predicate 查找匹配谓词的邮件
      Parameters:
      predicate - the filter predicate | 过滤谓词
      Returns:
      matching emails | 匹配的邮件列表
    • hasSentTo

      public boolean hasSentTo(String recipient)
      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

      public void simulateFailure(Predicate<Email> failureSimulator)
      Set failure simulator - emails matching the predicate will throw an exception 设置失败模拟器 - 匹配谓词的邮件将抛出异常
      Parameters:
      failureSimulator - the failure predicate, or null to disable | 失败谓词,null禁用
    • clearFailureSimulator

      public void clearFailureSimulator()
      Remove failure simulator 移除失败模拟器
    • close

      default void close()
      Close the sender and release resources 关闭发送器并释放资源