### Question:
AttachmentMimeBodyPartFactory extends AbstractMimeBodyPartFactory<Attachment> implements Initializable { @Override public MimeBodyPart create(Attachment attachment, Map<String, Object> parameters) throws MessagingException { MimeBodyPart attachmentPart = new MimeBodyPart(); DataSource source = createTemporaryAttachmentDataSource(attachment, attachmentPart); attachmentPart.setDataHandler(new DataHandler(source)); attachmentPart.setHeader("Content-Type", attachment.getMimeType()); attachmentPart.setHeader("Content-ID", "<" + attachment.getFilename() + ">"); attachmentPart.setFileName(attachment.getFilename()); addHeaders(attachmentPart, parameters); return attachmentPart; }  @Override void initialize(); @Override MimeBodyPart create(Attachment attachment, Map<String, Object> parameters); static final String TMP_ATTACHMENT_LOCATION_FILE_HEADER; }### Answer:
@Test void createAttachmentBodyPart() throws Exception { Attachment attachment = mock(Attachment.class); when(attachment.getContentInputStream()).thenReturn(new ByteArrayInputStream("Lorem Ipsum".getBytes())); when(attachment.getFilename()).thenReturn("image.png"); when(attachment.getMimeType()).thenReturn("image/png"); MimeBodyPart part = this.attachmentMimeBodyPartFactory.create(attachment, Collections.emptyMap()); assertEquals("<image.png>", part.getContentID()); assertTrue(part.getContentType().startsWith("image/png")); assertTrue(part.getFileName().matches("image\\.png")); assertEquals("Lorem Ipsum", IOUtils.toString(part.getDataHandler().getInputStream(), "UTF-8")); }
@Test void createAttachmentBodyPartWithHeader() throws Exception { Attachment attachment = mock(Attachment.class); when(attachment.getContentInputStream()).thenReturn(new ByteArrayInputStream("Lorem Ipsum".getBytes())); when(attachment.getFilename()).thenReturn("image.png"); when(attachment.getMimeType()).thenReturn("image/png"); Map<String, Object> parameters = Collections.singletonMap("headers", Collections.singletonMap("Content-Transfer-Encoding", "quoted-printable")); MimeBodyPart part = this.attachmentMimeBodyPartFactory.create(attachment, parameters); assertEquals("<image.png>", part.getContentID()); assertTrue(part.getContentType().startsWith("image/png")); assertTrue(part.getFileName().matches("image\\.png")); assertArrayEquals(new String[] { "quoted-printable" }, part.getHeader("Content-Transfer-Encoding")); assertEquals("Lorem Ipsum", IOUtils.toString(part.getDataHandler().getInputStream(), "UTF-8")); }
@Test void createAttachmentBodyPartWhenWriteError() { Attachment attachment = mock(Attachment.class); when(attachment.getFilename()).thenReturn("image.png"); when(attachment.getContentInputStream()).thenThrow(new RuntimeException("error")); Throwable exception = assertThrows(MessagingException.class, () -> this.attachmentMimeBodyPartFactory.create(attachment, Collections.emptyMap())); assertEquals("Failed to save attachment [image.png] to the file system", exception.getMessage()); }