Pete Pete - 1 year ago 105
Java Question

Testing for sent email - Dumbster and greenmail not catching sent JavaMail

I'm trying to integrate Dumbster to test our JavaMail based notifier for outgoing emails. The emails get sent but in my test Dumbster does not pick them up. I'm not sure if I need additional configuration to make this work buton the dumbster homepage it says, it will listen automatically for mail sent through smtp on port 25.

This is our java mail setup:

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="" />
<property name="port" value="25" />
<property name="username" value="theUserName" />
<property name="password" value="thePassword" />

<property name="javaMailProperties">
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.starttls.enable">true</prop>

Our Mailer class just injects the JavaMailer:

public class OurMailer {

private MailSender mailSender;

public void sendMail(String from, String to, String subject, String msg) {

SimpleMailMessage message = new SimpleMailMessage();


The test is quite straight forward as they demonstrate on their page:

private OurMailer ourMailer;

public void ourMailer_should_send_mail() {
SimpleSmtpServer server = SimpleSmtpServer.start();
Assert.assertTrue(server.getReceivedEmailSize() == 1);

As I said, the mail gets sent but the Assert fails.

Any ideas?

BTW: I also tried Greenmail but with the same result:

maven slightly different:


The test

GreenMail greenMail = new GreenMail(); //uses test ports by default
// send mail
Assert.assertEquals("subject", greenMail.getReceivedMessages()[0].getSubject());

Answer Source

The test fails because the config says "Send the mail to the SMTP server" but you want to send it to the SimpleSmtpServer which runs on localhost

Use a System property for the host parameter of the mailSender bean and set it to localhost when you run a test.

That said, I suggest to split the test into two. The first test should just make sure that the method is called with the correct parameters. That way you will know that the code tries to send mails at the correct time; there is little point in testing SMTP authentication, your mail server, the mail infrastructure and the network in a unit test - all these are tested by their respective manufacturers.

A second test should test just the method sendMail() method. Put that test into a test suite which can be run manually. What you want to know here is whether you correctly set up and use the MailSender API. Unless you change the code in the sendMail() method, you don't have to run this test at all.

This will speed up your unit test and get rid of many unnecessary dependencies which can cause the test to fail even though your code works correctly.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download