package es.devel.mail.impl;
import es.devel.mail.MailService;
import es.devel.mail.exception.MailServiceException;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import java.util.Properties;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
/**
* User: fran @ DEVEL C.S.I. (http://www.devel.es)
* Date: 17/01/12
* Time: 22:53
*/
public class MailServiceImplTest {
@Mock
private JavaMailSenderImpl sender;
@Before
public void setup() {
this.sender = mock(JavaMailSenderImpl.class);
this.sender = new JavaMailSenderImpl();
this.sender.setDefaultEncoding("ISO-8859-1");
this.sender.setHost("smtp.mydomain.com");
this.sender.setPort(25);
this.sender.setUsername("account@domain.com");
this.sender.setPassword("mypassword");
Properties smtpProps = new Properties();
smtpProps.put("mail.smtp.auth", true);
smtpProps.put("mail.smtp.starttls.enable", true);
this.sender.setJavaMailProperties(smtpProps);
}
@Test(expected = MailServiceException.class)
public void testSendMailWithNoRecipients() throws Exception {
//Arrange
MailService mailService = new MailServiceImpl(this.sender);
//Act
mailService.sendMail(null, "sender@domain.com", "Sender name", "The subject", "The body");
//Assert
fail("Exception should be thrown");
}
@Test(expected = MailServiceException.class)
public void testSendMailWithNoSenderEmail() throws Exception {
//Arrange
MailService mailService = new MailServiceImpl(this.sender);
//Act
mailService.sendMail(new String[2], "", "Sender name", "The subject", "The body");
//Assert
fail("Exception should be thrown");
}
@Test(expected = MailServiceException.class)
public void testSendMailWithNoSenderName() throws Exception {
//Arrange
MailService mailService = new MailServiceImpl(this.sender);
//Act
mailService.sendMail(new String[2], "sender@domain.com", null, "The subject", "The body");
//Assert
fail("Exception should be thrown");
}
}