package sk.vrto.service.email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import sk.vrto.domain.Contact;
import sk.vrto.domain.Email;
import sk.vrto.domain.EmailAccount;
import sk.vrto.service.crypto.SaltGenerator;
import sk.vrto.service.crypto.SaltedAESStringEncrypter;
import sk.vrto.service.crypto.StringEncrypter;
import javax.mail.MessagingException;
import static org.mockito.Matchers.anyObject;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.spy;
public class EmailServiceTest {
private EmailService emailService;
private StringEncrypter encrypter;
@BeforeMethod
protected void setUp() {
encrypter = new SaltedAESStringEncrypter(new SaltGenerator());
emailService = new EmailServiceImpl(
SmtPropertiesFactory.hotmailProperties(), encrypter);
}
@Test
public void testSend() throws MessagingException, EmailException {
emailService = spy(emailService);
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
System.out.println("Transport mocked");
return null;
}
}).when((EmailServiceImpl) emailService).doSend((SimpleEmail) anyObject());
// try out JavaMail assembling
emailService.send(
new EmailAccount("test@test.sk", encrypter.encrypt("skynet")),
new Email(new Contact("miso", "miso@miso.sk", null), "title", "content"));
// sending is mocked
// no exception should be thrown
}
}