Search code examples
javaspringunit-testingtestinggreenmail

Test if the mail sent sucessfully using Java


I have a mailingService who sends the mail:

@Component
public class MailingServiceImpl implements MailingService{

    @Autowired
    JavaMailSenderImpl mailSender;

    @Override
    public void sendMail(String sender, String receiver, String subject, String content) {

        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(sender);
        message.setTo(receiver);
        message.setSubject(subject);
        message.setText(content);
        mailSender.send(message);
    }
}

and I'm writing a test for this void sendMail(...) and I need to check if the mail was sent successfully. I found GreenMail that Im currently trying to use for my test, here is the code:

public class MailingServiceImplTest {

    private MailingServiceImpl mailingServiceImpl;

    @Autowired
    private JavaMailSenderImpl mailSender;

    @PostConstruct
    public void testConnection() {
        try {
            mailSender.testConnection();
        } catch(MessagingException ex) {
            throw new IllegalStateException("[Warning] Mail server is not available. " + ex);
        }
    }

    @Rule
    public final GreenMailRule greenMail = new GreenMailRule(ServerSetupTest.SMTP);

    @Test
    public void testSendMail() throws IOException {
        GreenMailUtil.sendTextEmailTest(receiver, sender, subject, content);
        // GreenMailUtil.sendTextEmailTest(to, from, subject, body);  -- here I could use the GreenMailUtil sendTextEmailTest, but I need to use my void sendMail function
        MimeMessage[] emails = greenMail.getReceivedMessages();
//      assertEquals(1, emails.length);
    }

Any ideas how to test my method? GreenMail (like example 'Using JUint (rule based setup)' section) isn't the only tool I tried, tried Dumbster (like example below, but sendMessage requires the strings as parameters, I can't pass my method. am I doing something wrong? Help

EDIT

My setup for JavaMailSenderImpl:

    @Bean
    public JavaMailSenderImpl mailSenderService(MailingServiceImpl mailingServiceImplementation,
            @Value("${mail.host}") String host,
            @Value("${mail.port}") int port,
            @Value("${mail.username}") String username,
            @Value("${mail.password}") String password) {
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost(host);
        mailSender.setPort(port);
        mailSender.setUsername(username);
        mailSender.setPassword(password);
        return mailSender;
    }

just recently found in debug that @Autowired MailingServiceImpl is null..but why? I don't really understand.


Solution

  • Greenmail and the like are starting a local MailService which can be accessed from your application. I haven't worked with it yet so don't take the code as is, but from my point of view your test should look sth. like this:

    @Autowired
    private MailingServiceImpl mailingServiceImpl;
    
    @Rule
    public final GreenMailRule greenMail = new GreenMailRule(ServerSetupTest.SMTP);
    
    @Test
    public void testSendMail() throws IOException {
        mailingServiceImpl.sendMail("sender", "receiver", "subject", "content");
        MimeMessage[] emails = greenMail.getReceivedMessages();
        assertEquals(1, emails.length);
    }
    

    Of course your JavaMailSender has to be configured properly to use your local GreenMail-SMTP service and of course your spring test setup must be correct (assuming you want to use spring).

    When using spring boot a config like this should be sufficient:

    spring.mail.host=localhost
    spring.mail.port=3025 #Default port of greenmail?!