### Question:
EmailService { public void send(@NonNull NotificationType type, @NonNull Recipient recipient, @Nullable String attachment) throws MessagingException { Validate.notNull(type); Validate.notNull(recipient); String filename = type.getAttachment() != null ? env.getProperty(type.getAttachment()) : null; Validate.validState(StringUtils.isNotBlank(filename) || StringUtils.isAllBlank(filename, attachment), "If an attachment is specified then its filename must not be blank"); String subject = env.getRequiredProperty(type.getSubject()); String text = MessageFormat.format(env.getRequiredProperty(type.getText()), recipient.getUsername()); MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setTo(recipient.getEmail()); helper.setSubject(subject); helper.setText(text); if (StringUtils.isNotBlank(attachment)) { helper.addAttachment(filename, new ByteArrayResource(attachment.getBytes())); } mailSender.send(message); logger.info("Email notification of type {} sent to {}", type, recipient.getEmail()); }  void send(@NonNull NotificationType type, @NonNull Recipient recipient, @Nullable String attachment);  }### Answer:
@Test void shouldFailToSendEmailIfAttachmentFilenameNotSetButAttachmentPassed() { Environment env = new MockEnvironment() .withProperty(NotificationType.BACKUP.getSubject(), SUBJECT) .withProperty(NotificationType.BACKUP.getText(), TEXT); EmailService emailService = new EmailService(mailSender, env); assertThatExceptionOfType(IllegalStateException.class) .isThrownBy(() -> emailService.send(NotificationType.BACKUP, recipient, ATTACHMENT)); }
@Test void shouldSendEmailWithAttachment() throws Exception { when(mailSender.createMimeMessage()) .thenReturn(new MimeMessage(Session.getDefaultInstance(new Properties()))); EmailService emailService = new EmailService(mailSender, mockEnvironment()); emailService.send(NotificationType.BACKUP, recipient, ATTACHMENT); verify(mailSender).send(messageCaptor.capture()); ParsedMimeMessageComponents components = MimeMessageParser.parseMimeMessage(messageCaptor.getValue()); assertThat(components.getSubject()).isEqualTo(SUBJECT); assertThat(components.getToAddresses()).extracting(Address::toString) .containsExactly(EMAIL); assertThat(components.getPlainContent()).isEqualTo(TEXT); Map<String, DataSource> attachments = components.getAttachmentList(); assertThat(attachments).hasSize(1); @Cleanup InputStream is = attachments.get(ATTACHMENT_FILENAME).getInputStream(); assertThat(IOUtils.toString(is, Charset.defaultCharset())).isEqualTo(ATTACHMENT); }
@Test void shouldSendEmailWithoutAttachmentIfNoAttachmentPassed() throws Exception { when(mailSender.createMimeMessage()) .thenReturn(new MimeMessage(Session.getDefaultInstance(new Properties()))); EmailService emailService = new EmailService(mailSender, mockEnvironment()); emailService.send(NotificationType.BACKUP, recipient, null); verify(mailSender).send(messageCaptor.capture()); assertThat(MimeMessageParser.parseMimeMessage(messageCaptor.getValue()).getAttachmentList()).isEmpty(); }
@Test void shouldFailToSendEmailIfAttachmentFilenameEmptyButAttachmentPassed() { EmailService emailService = new EmailService(mailSender, mockEnvironment()); assertThatExceptionOfType(IllegalStateException.class) .isThrownBy(() -> emailService.send(NotificationType.REMIND, recipient, ATTACHMENT)); }