Running Junit Email Tests Without Emails Actually Going Out

前端 未结 7 800
执笔经年
执笔经年 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.

提交回复
热议问题