Search code examples
javaspringcharacter-encodingjakarta-mail

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

Solution

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