Running Junit Email Tests Without Emails Actually Going Out

前端 未结 7 778
执笔经年
执笔经年 2020-12-23 09:46

I want to run unit tests (Junit) on some model classes that typically will send emails confirming that things happened. Is there a mock email server that you can use with u

相关标签:
7条回答
  • 2020-12-23 10:16

    I assume you're using Javamail and the problem is that javax.mail.Session is final and therefore can't be mocked.

    Seems like others have suggested you simply define your own 'mail session' interface and create an implementation that uses Javamail. In your tests you then simply inject a mock while in 'real-world-mode' you inject the Javamail implementation.

    Both JMock and EasyMock will support all the assertions you might want to make on the message you are sending and you testing is complete.

    As an aside, I generally try to avoid any out-of-process calls from within unit tests - it kills you when you're running the test suite frequently, which normally translates into it being run less and that's code base issues start to occur.

    0 讨论(0)
  • 2020-12-23 10:22

    I would take a look to GreenMail which is alive (apparently Dumbster is dead) with a lot of functionality and good examples.

    http://www.icegreen.com/greenmail/

    0 讨论(0)
  • 2020-12-23 10:26

    Phil Haack has a blog post about unit testing email sending, with a solution he coded around a freeware mail server.

    0 讨论(0)
  • 2020-12-23 10:33

    You can try JavaMail Mock2 https://github.com/salyh/javamail-mock2

    Its primarily focused on IMAP/POP3 but SMTP Mock is also available. Its available in maven central.

    Features

    • Support imap, imaps, pop3, pop3s, smtp, smtps
    • Supported for POP3: cast to POP3Folder, Folder.getUID(Message msg)
    • Supported for IMAP: cast to IMAPFolder, cast to UIDFolder, Subfolders, -Folder.getMessagesByUID(...), delete/rename folders, append messages
    • Support for SMTP: Mock Transport.send()
    • Unsupported for the moment: IMAP extensions like IDLE, CONDSTORE, ... and casts to POP3Message/IMAPMessage, store listeners
    0 讨论(0)
  • 2020-12-23 10:34

    Alternative answer: Dumbster is a fake SMTP server designed for testing against. It's written in Java.

    0 讨论(0)
  • 2020-12-23 10:37

    My solution was to wrap the mail server in a class which takes all the config options and has a send() method. In my tests, I'd mock this class and override send() with something that saves the current parameters for the assert.

    To test that the mail service itself works, send yourself a mail locally. Try hMail if you're on Windows.

    0 讨论(0)
提交回复
热议问题