If you are using maven, you can add following dependency to your project.
01 02 03 04 05 | < dependency > < groupId >org.unitils.mail</ groupId > < artifactId >unitils-mail</ artifactId > < version >1.0.2</ version > </ dependency > |
Please create unitils-local.properties, and add mail to unitils.modules. Code as following:
01 02 03 04 05 06 07 | unitils.modules =mail unitils.module.mail.className = org.unitils.mail.MailModule unitils.module.mail.runAfter = unitils.module.mail.enabled =true org.unitils.mail.port = 65212 |
To start and use the fake smpt server put this in your test class. This will start up the SMTP server before a test and cleans up, shuts it down after each test.
This has a performance cost but is nessacary to avoid any dependencies between tests
@TestSmtpServer private SmtpServer simpleSmtpServer;
The SmtpServer comes with 2 methods that allow you to inspect its state:
/** * This method give you the amount of mails that you've sent to the SMTP server. * */ int getReceivedEmailSize(); /** * This email sends you a list of all the mails you've sent to the fake SMTP server. * */ List<SmtpMessage> getReceivedEmail() throws IOException, MessagingException;
So to use it in your test:
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 | @RunWith (UnitilsJUnit4TestClassRunner. class ) public class MailModuleIntTest { @TestSmtpServer private SmtpServer simpleSmtpServer; @Test public void testSendEmail() throws javax.mail.MessagingException, IOException, javax.mail.MessagingException { // Some action that makes your code send a mail // make sure that the same port as org.unitils.mail.port is used // CODE OMITTED assertEquals( 1 , simpleSmtpServer.getReceivedEmailSize()); } } |
Notice that this will work the best if your test & your SUT (system under test) are running in the same JVM. If your application is deployed in some application server and is on the same machine as from where your test is launched, your test could still work. However because it is no longer running in one thread, hence it is running in different JVM's you will probably have to introduce some Thread.sleep before you do some asserts. If your application is deployed on a different machine as your from where you are launching your test, the test will fail. The TestSmtpServer is only bound to a port on the machine from where you launch your test.