Commit 05f2bf34 authored by Alexander Basov's avatar Alexander Basov Committed by Matija Obreza
Browse files

Mocked JavaMailSender in EmailServiceTest

parent 413f1df0
......@@ -9,6 +9,7 @@ import org.genesys2.server.test.PropertyPlacholderInitializer;
import org.genesys2.spring.config.HazelcastConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
......@@ -18,6 +19,7 @@ import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
......@@ -25,11 +27,12 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.isA;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = EmailServiceTest.Config.class, initializers = PropertyPlacholderInitializer.class)
@ActiveProfiles("dev")
@DirtiesContext(classMode= DirtiesContext.ClassMode.AFTER_CLASS)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public class EmailServiceTest {
private final Log LOG = LogFactory.getLog(EmailServiceTest.class);
......@@ -44,7 +47,7 @@ public class EmailServiceTest {
}
@Bean
public ThreadPoolTaskExecutor threadPoolTaskExecutor(){
public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
return new ThreadPoolTaskExecutor();
}
......@@ -54,19 +57,30 @@ public class EmailServiceTest {
}
@Bean
public EMailService eMailService(){
public EMailService eMailService() {
return new EMailServiceImpl();
}
@Bean
public JavaMailSender mailSender() {
return Mockito.mock(JavaMailSenderImpl.class);
}
}
@Value("${mail.user.from}")
String emailFrom;
@Value("${mail.requests.to}")
String emailTo;
@Autowired
private JavaMailSender mailSender;
@Autowired
private EMailService eMailService;
@Test
public void toEmailsTest(){
public void toEmailsTest() {
LOG.info("Start test-method toEmailsTest");
String[] emails = eMailService.toEmails("salexandrbasov@gmail.com,test@mail.ru;mail@gmail.com;alex@mail.com");
......@@ -79,8 +93,9 @@ public class EmailServiceTest {
LOG.info("Test toEmailsTest passed!");
}
@Test
public void toEmailsEmptyTest(){
public void toEmailsEmptyTest() {
LOG.info("Start test-method toEmailsEmptyTest");
String[] emails = eMailService.toEmails("");
......@@ -90,14 +105,17 @@ public class EmailServiceTest {
}
@Test
public void sendMailTest(){
public void sendMailTest() {
LOG.info("Start test-method sendMailTest");
String mailSubject = "mailSubject";
String mailBody = "mail body";
String mailTo = "salexandrbasov@gmail.com";
final String mailSubject = "mailSubject";
final String mailBody = "mail body";
final String mailTo = "salexandrbasov@gmail.com";
Mockito.doNothing().when(mailSender).send(isA(MimeMessagePreparator.class));
eMailService.sendMail(mailSubject, mailBody, "test@localhost");
eMailService.sendMail(mailSubject, mailBody, mailTo);
Mockito.verify(mailSender).send(isA(MimeMessagePreparator.class));
LOG.info("Test sendMailTest passed!");
}
......
......@@ -23,6 +23,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.cache.support.NoOpCacheManager;
import org.springframework.context.annotation.Bean;
......@@ -31,6 +32,8 @@ import org.springframework.context.annotation.PropertySource;
import org.springframework.data.domain.PageRequest;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
......@@ -41,6 +44,7 @@ import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.mail.internet.MimeMessage;
import java.util.ArrayList;
import java.util.List;
......@@ -90,7 +94,7 @@ public class EmailVerificationServiceTest {
@Bean
public JavaMailSender javaMailSender() {
return new JavaMailSenderImpl();
return Mockito.mock(JavaMailSenderImpl.class);
}
@Bean
......@@ -109,6 +113,15 @@ public class EmailVerificationServiceTest {
}
}
@Value("${mail.user.from}")
String emailFrom;
@Value("${mail.requests.to}")
String emailTo;
@Autowired
private JavaMailSender mailSender;
@Autowired
private EMailService eMailService;
......@@ -201,7 +214,18 @@ public class EmailVerificationServiceTest {
public void sendVerificationEmailTest() {
LOG.info("Start test-method sendVerificationEmailTest");
Mockito.doNothing().when(eMailService).sendMail("title for oneArticle", "<h1>Tho BOdy of oneArticle</h1", "salexandrbasov@gmail.com");
Mockito.doNothing().when(mailSender).send(new MimeMessagePreparator() {
@Override
public void prepare(MimeMessage mimeMessage) throws Exception {
final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, "UTF-8");
message.setFrom(emailFrom);
message.setTo(emailTo);
message.setSubject("title for oneArticle");
message.setText("<h1>Tho BOdy of oneArticle</h1", true);
}
});
emailVerificationService.sendVerificationEmail(userService.listUsers(new PageRequest(0, 1)).getContent().get(0));
Mockito.verify(eMailService).sendMail("title for oneArticle", "<h1>Tho BOdy of oneArticle</h1", "salexandrbasov@gmail.com");
LOG.info("Test sendVerificationEmailTest passed!");
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment