How do I test the JavaMailSender of Spring

Jordi P.S. picture Jordi P.S. · May 31, 2013 · Viewed 15.7k times · Source

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
        ...
    }
}

Answer

dimitrisli picture dimitrisli · May 31, 2013

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