### Question:
EmailService { public boolean sendRecoveryCode(String name, String email, String code) { HtmlEmail htmlEmail; try { htmlEmail = sendMailSsl.initializeMail(email); } catch (EmailException e) { ConsoleLogger.logException("Failed to create email for recovery code:", e); return false; } String message = replaceTagsForRecoveryCodeMail(settings.getRecoveryCodeEmailMessage(), name, code, settings.getProperty(SecuritySettings.RECOVERY_CODE_HOURS_VALID)); return sendMailSsl.sendEmail(message, htmlEmail); } @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 shouldSendRecoveryCode() throws EmailException { given(settings.getProperty(SecuritySettings.RECOVERY_CODE_HOURS_VALID)).willReturn(7); given(settings.getRecoveryCodeEmailMessage()) .willReturn("Hi <playername />, your code on <servername /> is <recoverycode /> (valid <hoursvalid /> hours)"); HtmlEmail email = mock(HtmlEmail.class); given(sendMailSsl.initializeMail(anyString())).willReturn(email); given(sendMailSsl.sendEmail(anyString(), any(HtmlEmail.class))).willReturn(true); boolean result = emailService.sendRecoveryCode("Timmy", "tim@example.com", "12C56A"); assertThat(result, equalTo(true)); verify(sendMailSsl).initializeMail("tim@example.com"); ArgumentCaptor<String> messageCaptor = ArgumentCaptor.forClass(String.class); verify(sendMailSsl).sendEmail(messageCaptor.capture(), eq(email)); assertThat(messageCaptor.getValue(), equalTo("Hi Timmy, your code on serverName is 12C56A (valid 7 hours)")); }
@Test public void shouldHandleMailCreationErrorForRecoveryCode() throws EmailException { given(sendMailSsl.initializeMail(anyString())).willThrow(EmailException.class); boolean result = emailService.sendRecoveryCode("Player", "player@example.org", "ABC1234"); assertThat(result, equalTo(false)); verify(sendMailSsl).initializeMail("player@example.org"); verify(sendMailSsl, never()).sendEmail(anyString(), any(HtmlEmail.class)); }
@Test public void shouldHandleFailureToSendRecoveryCode() throws EmailException { given(settings.getProperty(SecuritySettings.RECOVERY_CODE_HOURS_VALID)).willReturn(7); given(settings.getRecoveryCodeEmailMessage()).willReturn("Hi <playername />, your code is <recoverycode />"); EmailService sendMailSpy = spy(emailService); HtmlEmail email = mock(HtmlEmail.class); given(sendMailSsl.initializeMail(anyString())).willReturn(email); given(sendMailSsl.sendEmail(anyString(), any(HtmlEmail.class))).willReturn(false); boolean result = sendMailSpy.sendRecoveryCode("John", "user@example.com", "1DEF77"); 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 John, your code is 1DEF77")); }