Class EmailMailpit

java.lang.Object
net.bugreaper.modules.email.EmailMailpit
All Implemented Interfaces:
MailPitInterface

public class EmailMailpit extends Object implements MailPitInterface
EmailMailpit helper that provides a common API for operating with Mailpit, an email and SMTP testing tool.

Class contains methods for interacting with Mailpit via its API without authentication. It provides simple wrappers around the REST module.

It is recommended to use a single instance: EmailMailpit mailpit = EmailMailpit.getInstance();

Since:
1.0.0
  • Constructor Details

    • EmailMailpit

      public EmailMailpit(String url, int port)
      Creates an EmailMailpit helper with the specified connection settings.
      Parameters:
      url - host of Mailpit "http://your-mailpit-host"
      port - port of Mailpit
    • EmailMailpit

      public EmailMailpit()
      Constructs a EmailMailpit client using YAML configuration.

      Loads configuration values from a YAML file.

      Default file: bugreaper.yml

      Custom file: using -DbugreaperEnv=test loads bugreaper-test.yml

       modules:
         mailpit:
           url: http://localhost
           port: 8025
           await: 600 # (optional)
       

      Missing required keys will result in configuration errors. Missing optional keys will fall back to predefined defaults.

      Throws:
      IllegalArgumentException - if the configuration contains invalid values
  • Method Details

    • getInstance

      public static EmailMailpit getInstance()
      Returns the instance of EmailMailpit with config builder EmailMailpit().

      This implementation is thread-safe using method-level synchronization.

      Returns:
      the shared instance of EmailMailpit
      Throws:
      IllegalArgumentException - if the configuration contains invalid values
      See Also:
    • setAwaitMs

      public EmailMailpit setAwaitMs(int awaitMs)
      Description copied from interface: MailPitInterface
      Configures the global await timeout for assertions that use await.
      Specified by:
      setAwaitMs in interface MailPitInterface
      Parameters:
      awaitMs - await timeout in milliseconds
      Returns:
      this instance for method chaining
    • getConfigSummary

      public String getConfigSummary()
      Description copied from interface: MailPitInterface
      Returns and logs (at INFO level) a human-readable summary of all resolved configuration values.

      The summary includes values loaded from the YAML configuration file as well as any fields overridden programmatically after construction. Optional fields that were not present in the configuration and resolved via default values may also be included.

      Specified by:
      getConfigSummary in interface MailPitInterface
      Returns:
      String with summary
    • cleanAllMailboxes

      @Step("(Email-mp) Clean all mailboxes") public void cleanAllMailboxes()
      Description copied from interface: MailPitInterface
      Deletes all messages from all mailboxes.
      Specified by:
      cleanAllMailboxes in interface MailPitInterface
    • cleanMailbox

      @Step("(Email-mp) Clean mailbox: \'{mailbox}\'") public void cleanMailbox(String mailbox)
      Description copied from interface: MailPitInterface
      Deletes all messages from the mailbox.
      Specified by:
      cleanMailbox in interface MailPitInterface
      Parameters:
      mailbox - mailbox address
    • getLatestEmailInMailbox

      @Step("(Email-mp) Get latest email from mailbox: \'{mailbox}\'") public AssertableMailpitResponse getLatestEmailInMailbox(String mailbox)
      Description copied from interface: MailPitInterface
      Returns the latest message from the mailbox in Mailpit format.
      Specified by:
      getLatestEmailInMailbox in interface MailPitInterface
      Parameters:
      mailbox - mailbox address
      Returns:
      AssertableMailpitResponse

      Usage Example:

      
       mailpit.getLatestEmailInMailbox("my_email@gmail.com")
               .seeEmailContainsFrom("sender@gmail.com")
               .seeEmailAttachmentsCountExactly(2);
       

      can be provided multiple asserts

    • getAllEmailsCount

      @Step("(Email-mp) Get all emails count") public int getAllEmailsCount()
      Description copied from interface: MailPitInterface
      Returns the number of messages in all mailboxes.
      Specified by:
      getAllEmailsCount in interface MailPitInterface
      Returns:
      number of messages in all mailboxes
    • getEmailsCountInMailbox

      @Step("(Email-MP) Get email count in mailbox \'{mailbox}\'") public int getEmailsCountInMailbox(String mailbox)
      Description copied from interface: MailPitInterface
      Returns the number of messages in the mailbox.
      Specified by:
      getEmailsCountInMailbox in interface MailPitInterface
      Parameters:
      mailbox - mailbox address
      Returns:
      number of messages in the mailbox
    • unsetChaos

      @Step("(Email-mp) Unset CHAOS") public void unsetChaos()
      Description copied from interface: MailPitInterface
      Disables predefined chaos errors in the Mailpit server.

      Works only when MP_ENABLE_CHAOS=true.

      Specified by:
      unsetChaos in interface MailPitInterface
    • setChaosAuthentication

      public void setChaosAuthentication(int code)
      Description copied from interface: MailPitInterface
      Configures an authentication error in the Mailpit server for subsequent email sends.

      Works only when MP_ENABLE_CHAOS=true and MP_SMTP_AUTH=true.

      Specified by:
      setChaosAuthentication in interface MailPitInterface
      Parameters:
      code - error code between 400 and 599
    • setChaosRecipient

      public void setChaosRecipient(int code)
      Description copied from interface: MailPitInterface
      Configures a recipient error in the Mailpit server for subsequent email sends.

      Works only when MP_ENABLE_CHAOS=true.

      Specified by:
      setChaosRecipient in interface MailPitInterface
      Parameters:
      code - error code between 400 and 599
    • setChaosSender

      public void setChaosSender(int code)
      Description copied from interface: MailPitInterface
      Configures a sender error in the Mailpit server for subsequent email sends.

      Works only when MP_ENABLE_CHAOS=true.

      Specified by:
      setChaosSender in interface MailPitInterface
      Parameters:
      code - error code between 400 and 599
    • seeAllEmailsCountExactly

      @Step("(Email-mp)[ASSERT] All mailboxes have exactly <{expectedCount}> emails") public void seeAllEmailsCountExactly(int expectedCount)
      Description copied from interface: MailPitInterface
      Asserts that the number of messages in all mailboxes equals the expected count.

      Uses await.

      Specified by:
      seeAllEmailsCountExactly in interface MailPitInterface
      Parameters:
      expectedCount - expected number of messages
    • seeEmailsInMailboxCountExactly

      @Step("(Email-mp)[ASSERT] Mailbox: \'{mailbox}\' has exactly <{expectedCount}> emails") public void seeEmailsInMailboxCountExactly(String mailbox, int expectedCount)
      Description copied from interface: MailPitInterface
      Asserts that the number of messages in specific mailbox equals the expected count.

      Uses await.

      Specified by:
      seeEmailsInMailboxCountExactly in interface MailPitInterface
      Parameters:
      mailbox - mailbox address
      expectedCount - expected number of messages
    • seeMailboxIsEmpty

      @Step("(Email-mp)[ASSERT] Mailbox: \'{mailbox}\' is empty") public void seeMailboxIsEmpty(String mailbox)
      Description copied from interface: MailPitInterface
      Asserts that the mailbox is empty.

      Uses await.

      Specified by:
      seeMailboxIsEmpty in interface MailPitInterface
      Parameters:
      mailbox - mailbox address
    • seeMailboxIsNotEmpty

      @Step("(Email-mp)[ASSERT] Mailbox: \'{mailbox}\' is not empty") public void seeMailboxIsNotEmpty(String mailbox)
      Description copied from interface: MailPitInterface
      Asserts that the mailbox is not empty.

      Uses await.

      Specified by:
      seeMailboxIsNotEmpty in interface MailPitInterface
      Parameters:
      mailbox - mailbox address