MessageSender { @VisibleForTesting static Mail buildMail(MetaDataWithContent message, Envelope envelope) throws MessagingException { String name = message.getMessageId().serialize(); return MailImpl.builder() .name(name) .sender(envelope.getFrom().asOptional().orElseThrow(() -> new RuntimeException("Sender is mandatory"))) .addRecipients(envelope.getRecipients()) .mimeMessage(toMimeMessage(name, message.getContent())) .build(); } @Inject  MessageSender(MailSpool mailSpool); void sendMessage(MetaDataWithContent message,
                            Envelope envelope,
                            MailboxSession session); void sendMessage(MessageId messageId,
                            Mail mail,
                            MailboxSession session);  }
@Test void buildMailShouldThrowWhenNullMailboxMessage() throws Exception { MetaDataWithContent message = null; assertThatThrownBy(() -> MessageSender.buildMail(message, envelope)).isInstanceOf(NullPointerException.class); }
@Test void buildMailShouldThrowWhenNullJmapMessage() throws Exception { Envelope envelope = null; assertThatThrownBy(() -> MessageSender.buildMail(message, envelope)).isInstanceOf(NullPointerException.class); }
@Test void buildMailShouldGenerateMailWithExpectedProperties() throws Exception { String expectedName = jmapMessage.getId().serialize(); MailAddress expectedSender = new MailAddress("me@example.com"); Collection<MailAddress> expectedRecipients = ImmutableSet.of( new MailAddress("1@example.com"), new MailAddress("3@example.com"), new MailAddress("2@example.com"), new MailAddress("4@example.com")); Mail actual = MessageSender.buildMail(message, envelope); assertThat(actual.getName()).isEqualTo(expectedName); assertThat(actual.getMaybeSender()).isEqualTo(MaybeSender.of(expectedSender)); assertThat(actual.getRecipients()).containsAll(expectedRecipients); }