@Test
  public void saveMessageThrowsMailStoreExceptionWhenError() throws Exception {
    Environment environment = this.mocker.getInstance(Environment.class);
    when(environment.getPermanentDirectory()).thenReturn(new File(TEMPORARY_DIRECTORY));

    String batchId = UUID.randomUUID().toString();
    String messageId = "ar1vm0Wca42E/dDn3dsH8ogs3/s=";

    ExtendedMimeMessage message = mock(ExtendedMimeMessage.class);
    when(message.getUniqueMessageId()).thenReturn(messageId);

    this.thrown.expect(MailStoreException.class);
    this.thrown.expectMessage(
        "Failed to save message (id [" + messageId + "], batch id [" + batchId + "]) into file");

    when(message.getContent())
        .thenReturn("Lorem ipsum dolor sit amet, consectetur adipiscing elit");
    doThrow(new IOException()).when(message).writeTo(any(OutputStream.class));
    this.mocker.getComponentUnderTest().save(batchId, message);
  }
  @Test
  public void saveMessage() throws Exception {
    String batchId = UUID.randomUUID().toString();

    ExtendedMimeMessage message = new ExtendedMimeMessage();
    message.setText("Lorem ipsum dolor sit amet, consectetur adipiscing elit");

    this.mocker.getComponentUnderTest().save(batchId, message);
    String messageId = message.getMessageID();

    File tempDir = new File(TEMPORARY_DIRECTORY);
    File batchDirectory =
        new File(
            new File(tempDir, this.mocker.getComponentUnderTest().ROOT_DIRECTORY),
            URLEncoder.encode(batchId, "UTF-8"));
    File messageFile =
        new File(batchDirectory, URLEncoder.encode(message.getUniqueMessageId(), "UTF-8"));
    InputStream in = new FileInputStream(messageFile);
    String messageContent = IOUtils.toString(in);

    assertTrue(messageContent.contains("Message-ID: " + messageId));
    assertTrue(messageContent.contains("Lorem ipsum dolor sit amet, consectetur adipiscing elit"));
  }