Working with a Java Mail Server for Testing

前端 未结 5 1830
南笙
南笙 2021-01-02 03:33

I\'m in the process of testing an application that takes mail out of a mailbox, performs some action based on the content of that mail, and then sends a response mail depend

5条回答
  •  长情又很酷
    2021-01-02 04:07

    The Mock-JavaMail project

    I came across it when developing a plugin for Jenkins, and it's been a dream to use!

    Just drop the dependency into your project, and you're ready to go (I'll let Kohsuke explain how to set it up and use it).

    If you're impatient, here's a quick example of how it's used:

    Example:

    // Setup test: add mail to inbox
    Mailbox tmp = Mailbox.get("foo@bar.com");
    tmp.add(/* your javax.mail.Message */)
    assertEquals 1, tmp.size()
    
    // Connect to the inmemory mailbox using "imap"
    Session session = Session.getInstance(System.getProperties(), null);
    Store store = session.getStore('imap');
    store.connect("bar.com","foo","anything");
    
    // Check the mail exists!
    Folder inbox = store.getFolder("INBOX");
    inbox.open(Folder.READ_ONLY);
    assertEquals 1, inbox.getMessageCount()
    store.close();
    

提交回复
热议问题