mardi 31 mars 2015

Mokito mail API testing Issue

jvnet.mock_javamail.Mailbox; for testing email But the email goes to actual id agniest the memory Mailbox


Here is my code



@Before
public void init() {
messageTemplateService =mock(MessageTemplateService.class);
props=mock(Properties.class);
emailProperties=mock(EmailProperties.class);
//emailProperties.setP
emailService.setEmailProperties(emailProperties);
emailService.setMessageTemplateService(messageTemplateService);
Mailbox.clearAll();
}

@Test
public void testSend() throws MessagingException, IOException {
//Mailbox.clearAll();
when(emailProperties.getAdminTo()).thenReturn("pritam.pritam176@gmail.com");
when(emailProperties.getSenderEmail()).thenReturn("pritam.pkm1989@gmail.com");
when(emailProperties.getSender()).thenReturn("pritam");
//Mailbox mailbox = Mailbox.get(emailProperties.getAdminTo());
//EmailServiceImpl emailService = new EmailServiceImpl();

Properties props =new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.user", "pritam.pritam176@gmail.com");
props.put("mail.smtp.password", "9439586575");
props.put("mail.smtp.port", 587);
props.put("mail.smtp.auth", true);
props.put("mail.debug", true);

when(emailProperties.getProps()).thenReturn(props);
when(emailProperties.getHost()).thenReturn("smtp.gmail.com");
when(emailProperties.getUserName()).thenReturn("pritam.pritam176@gmail.com");
when(emailProperties.getPassword()).thenReturn("9439586575");

MessageTemplate temp= new MessageTemplate();
temp.setBody("Your Friend ${friend} want to see this link");
temp.setSubject("Link");
when(messageTemplateService.getMessageTemplateById("2")).thenReturn(temp);
MessageTemplate mTemplate=messageTemplateService.getMessageTemplateById("2");



String to=emailProperties.getAdminTo();
String from = emailProperties.getSenderEmail();
String subject=mTemplate.getSubject();
String contentType= "text/html";
String body=mTemplate.getBody();

emailService.sendEmail(to, subject, body, contentType);
Mailbox mailbox = Mailbox.get(emailProperties.getAdminTo());
assertEquals(1, mailbox.getNewMessageCount());
assertFromEquals(mailbox.get(0), from);
assertToEquals(mailbox.get(0), emailProperties.getAdminTo());
assertSubjectEquals(mailbox.get(0), "Subject: testSend");
assertBodyEquals(mailbox.get(0), "Body: testSend"); }



  • what am i did wrong??Please help me Mailbox mailbox = Mailbox.get(emailProperties.getAdminTo()); line should return 1 but it return 0.


Aucun commentaire:

Enregistrer un commentaire