Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I test the JavaMailSender of Spring

I have a service that has injected the JavaMailSender. My service configures it and sends a mail. I'd like to intercept the raw mail to ensure the information is the correct. I'd like to do that in a JUnit.

How would you guys do that?

@Service
public class MyServiceImpl {

    @Autowired
    private JavaMailSender _mailSender;

    public void sendMail(String to, String body, String subject){
        ...
        _mailSender.something
        ...
    }
}
like image 654
Jordi P.S. Avatar asked May 31 '13 10:05

Jordi P.S.


3 Answers

I've done it using GreenMail. Take a look at my blog post about it where you'll also find a working example.

like image 120
dimitrisli Avatar answered Sep 30 '22 15:09

dimitrisli


You can use a test SMTP server, like Dumbster. See the example below:

@Test
    public void sendSimpleEmailWithCC() {
        // Runs a Dumbster simple SMTP server - default config
        SimpleSmtpServer server = SimpleSmtpServer.start();
        String from = "[email protected]";
        String to = "[email protected]";
        String messageText = "Good message";
        String title = "Test message";
        String cc = "[email protected]";
        Assert.assertTrue(mailSender
                .sendEmail(from, to, cc, title, messageText));
        server.stop();
        Assert.assertTrue(server.getReceivedEmailSize() == 1);
        Iterator emailIter = server.getReceivedEmail();
        SmtpMessage email = (SmtpMessage) emailIter.next();
        Assert.assertTrue(email.getHeaderValue("From").equals(from));
        Assert.assertTrue(email.getHeaderValue("To").equals(to));
        Assert.assertTrue(email.getHeaderValue("Cc").equals(cc));
        Assert.assertTrue(email.getHeaderValue("Subject")
                .equals("Test message"));
        Assert.assertTrue(email.getBody().equals(messageText));
    }
like image 21
TrueDub Avatar answered Sep 30 '22 17:09

TrueDub


If your goal is to use just Junit/Mockito and test how MimeMessage was formed before sending then the configuration below should be sufficient:

public class EmailServiceTest {

    private EmailServiceImpl emailServiceImpl;

    private JavaMailSender javaMailSender;

    private MimeMessage mimeMessage;

    @Before
    public void before() {
        mimeMessage = new MimeMessage((Session)null);
        javaMailSender = mock(JavaMailSender.class);
        when(javaMailSender.createMimeMessage()).thenReturn(mimeMessage);
        emailServiceImpl = new EmailService(javaMailSender);
    }

    @Test
    public void emailTest() {
        String recipient = "[email protected]"
        EmailRequest request = new EmailRequest();
        request.setRecipient(recipient);
        emailServiceImpl.send(request);
        assertEquals(recipient, mimeMessage.getRecipients(RecipientType.TO)[0].toString());
    }
}
like image 41
Justinas Jakavonis Avatar answered Sep 30 '22 17:09

Justinas Jakavonis