MailerService { public void send(String to, String subject, String html) { MimeMessage message = mailSender.createMimeMessage(); try { MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8"); message.setContent(html, "text/html"); helper.setTo(to); helper.setSubject(subject); mailSender.send(message); } catch (MessagingException e) { logger.error(String.format("Not possible to send email [%s] to %s", subject, to), e); throw new RuntimeException("Error when system send the mail " + subject, e); } }  void send(String to, String subject, String html);  }
@Test(expected = RuntimeException.class) public void should_throwRunTimeException_when_mail_error() throws Exception { doThrow(MessagingException.class).when(mailSender).send(any(SimpleMailMessage.class)); mailerService.send("to", "subject", "<h1>Hello</h1>"); }