0
  • I write a email testing using greenmail and JUNIT with mokito but

  • when I use
    when(emailproperties.getUsername()).thenReturn("abc@gmail.com");

  • show null

  • Here is my code

public class EmailServiceImplTest {

@InjectMocks
EmailServiceImpl emailServiceImpl = new EmailServiceImpl();
@Mock
private MessageTemplateService messageTemplateService;
@Mock
private EmailProperties emailProperties;
//private static final Logger LOGGER = Logger.getLogger(EmailServiceImplTest.class);

private GreenMail greenMail;
private static final String USER_NAME = "hascode";
    private static final String EMAIL_USER_ADDRESS = "hascode@localhost";
    private static final String EMAIL_TO = "someone@localhost.com";
    private static final String EMAIL_SUBJECT = "Test E-Mail";
    private static final String EMAIL_TEXT = "This is a test e-mail.";
    private static final String LOCALHOST = "localhost";
    private static final String USER_PASSWORD = "abcdef123";

@Before
public void testSmtpInit() {
    MockitoAnnotations.initMocks(this);
    greenMail = new GreenMail(ServerSetupTest.SMTP);
    greenMail.start();
    greenMail.setUser(EMAIL_USER_ADDRESS, USER_NAME, USER_PASSWORD);
}


@Test
public void sendContactEmail() {
    MessageTemplate mock_template=new MessageTemplate();
    mock_template.setId(2);
    mock_template.setBody("Hi ${name} want to contact");
    mock_template.setSubject("Contact EMAIL");
    ContactDTO mock_Dto=new ContactDTO();
    mock_Dto.setFirstName("abc");
    mock_Dto.setLastName("xyz");
     Properties mock_props = System.getProperties();
     mock_props.put("mail.smtp.host", LOCALHOST);
     mock_props.put("mail.smtp.auth", "true");
     mock_props.put("mail.smtp.port", ServerSetupTest.SMTP.getPort());
     mock_props.put("mail.debug", "true");

    when(messageTemplateService.getMessageTemplateById("2")).thenReturn(mock_template);
    emailProperties.setAdminTo("abc@mail.com");
    when(emailProperties.getAdminTo()).thenReturn("abc@mail.com");
    when(emailProperties.getContactMsgKey()).thenReturn("2");
    when(emailProperties.getProps()).thenReturn(mock_props);
    when(emailProperties.getSenderEmail()).thenReturn(EMAIL_USER_ADDRESS);
    when(emailProperties.getSender()).thenReturn(USER_NAME);
    when(emailProperties.getHost()).thenReturn(LOCALHOST);
    when(emailProperties.getUserName()).thenReturn(EMAIL_USER_ADDRESS);
    when(emailProperties.getPassword()).thenReturn(USER_PASSWORD);


    emailServiceImpl.sendContactEmail(mock_Dto);
    MimeMessage[] messages = greenMail.getReceivedMessages();
    assertNotNull(messages);
    assertEquals(1, messages.length);


}
  • when I debug it show null what I am doing wronge
  • I don't quite understand what you're trying to ask. Is the problem that you expect for mockito to send a message? or is it because the messages variable is null? if it's the latter then, isn't because you haven't mock the `greenMail.getReceivedMessages()`. – kucing_terbang Apr 05 '15 at 13:43
  • my question is I mock the emailProperties but when i debug I see that the emailProperties is not caliing the when() return vakue. – PRITAM MOHAPATRA Apr 05 '15 at 14:16
  • I tried it but it did return the value that i have set before. How do you debug it? You're not directly referring to the field right? how about you try to inspect this function `emailproperties.getUsername()` and see what is the result. – kucing_terbang Apr 05 '15 at 15:22
  • set The Debug pointer in `sendContactEmail(mock_Dto); `implememtinon and see what are the value set but. `when(messageTemplateService.getMessageTemplateById("2")).thenReturn(mock_template);` this one working perfect but all the `when(emailProperties.getAdminTo()).thenReturn("abc@mail.com");` showing null value. – PRITAM MOHAPATRA Apr 06 '15 at 07:15

1 Answers1

0

Yes Done.Change to @spy @Mock private EmailProperties emailProperties;

  • and set the value as it is.
  • 15 days effort end to day.