### Question:
EmailService { public boolean sendPasswordMail(String name, String mailAddress, String newPass) { if (!hasAllInformation()) { ConsoleLogger.warning("Cannot perform email registration: not all email settings are complete"); return false; } HtmlEmail email; try { email = sendMailSsl.initializeMail(mailAddress); } catch (EmailException e) { ConsoleLogger.logException("Failed to create email with the given settings:", e); return false; } String mailText = replaceTagsForPasswordMail(settings.getPasswordEmailMessage(), name, newPass); File file = null; if (settings.getProperty(EmailSettings.PASSWORD_AS_IMAGE)) { try { file = generatePasswordImage(name, newPass); mailText = embedImageIntoEmailContent(file, email, mailText); } catch (IOException | EmailException e) { ConsoleLogger.logException( "Unable to send new password as image for email " + mailAddress + ":", e); } } boolean couldSendEmail = sendMailSsl.sendEmail(mailText, email); FileUtils.delete(file); return couldSendEmail; } @Inject  EmailService(@DataFolder File dataFolder, Server server, Settings settings, SendMailSsl sendMailSsl); boolean hasAllInformation(); boolean sendPasswordMail(String name, String mailAddress, String newPass); boolean sendVerificationMail(String name, String mailAddress, String code); boolean sendRecoveryCode(String name, String email, String code);  }### Answer:
@Test public void shouldSendPasswordMail() throws EmailException { given(settings.getPasswordEmailMessage()) .willReturn("Hi <playername />, your new password for <servername /> is <generatedpass />"); given(settings.getProperty(EmailSettings.PASSWORD_AS_IMAGE)).willReturn(false); HtmlEmail email = mock(HtmlEmail.class); given(sendMailSsl.initializeMail(anyString())).willReturn(email); given(sendMailSsl.sendEmail(anyString(), eq(email))).willReturn(true); boolean result = emailService.sendPasswordMail("Player", "user@example.com", "new_password"); assertThat(result, equalTo(true)); verify(sendMailSsl).initializeMail("user@example.com"); ArgumentCaptor<String> messageCaptor = ArgumentCaptor.forClass(String.class); verify(sendMailSsl).sendEmail(messageCaptor.capture(), eq(email)); assertThat(messageCaptor.getValue(), equalTo("Hi Player, your new password for serverName is new_password")); }
@Test public void shouldHandleMailCreationError() throws EmailException { doThrow(EmailException.class).when(sendMailSsl).initializeMail(anyString()); boolean result = emailService.sendPasswordMail("Player", "user@example.com", "new_password"); assertThat(result, equalTo(false)); verify(sendMailSsl).initializeMail("user@example.com"); verify(sendMailSsl, never()).sendEmail(anyString(), any(HtmlEmail.class)); }
@Test public void shouldHandleMailSendingFailure() throws EmailException { given(settings.getPasswordEmailMessage()).willReturn("Hi <playername />, your new pass is <generatedpass />"); given(settings.getProperty(EmailSettings.PASSWORD_AS_IMAGE)).willReturn(false); HtmlEmail email = mock(HtmlEmail.class); given(sendMailSsl.initializeMail(anyString())).willReturn(email); given(sendMailSsl.sendEmail(anyString(), any(HtmlEmail.class))).willReturn(false); boolean result = emailService.sendPasswordMail("bobby", "user@example.com", "myPassw0rd"); assertThat(result, equalTo(false)); verify(sendMailSsl).initializeMail("user@example.com"); ArgumentCaptor<String> messageCaptor = ArgumentCaptor.forClass(String.class); verify(sendMailSsl).sendEmail(messageCaptor.capture(), eq(email)); assertThat(messageCaptor.getValue(), equalTo("Hi bobby, your new pass is myPassw0rd")); }