【问题标题】:Loading .eml files into javax.mail.Messages将 .eml 文件加载到 javax.mail.Messages
【发布时间】:2011-02-16 10:14:37
【问题描述】:

我正在尝试对处理 javax.mail.Message 实例的方法进行单元测试。

我正在编写一个转换器来更改以不同格式到达的电子邮件,然后将其转换为一致的内部格式 (MyMessage)。这种转换通常取决于电子邮件的发件人地址或回复地址,而电子邮件的各个部分、主题以及发件人和回复地址将是创建新的MyMessage 所必需的。

我有一组原始电子邮件,它们在本地保存为.eml 文件,我想做一个单元测试,从类路径加载.eml 文件并将它们转换为javax.mail.Message 实例。这可能吗?如果可以,怎么做?

【问题讨论】:

    标签: unit-testing email mocking mockito jakarta-mail


    【解决方案1】:

    经过几次测试,我终于使用 MimeMessage(Session, InputStream) 公共构造函数成功加载了一条消息(与另一个响应中引用的基于文件夹的受保护的构造函数相反)。

    import java.io.FileInputStream;
    import java.io.InputStream;
    
    import javax.mail.internet.MimeMessage;
    
    public class LoadEML {
    
        public static void main(String[] args) throws Exception {
            InputStream is = new FileInputStream(args[0]);
            MimeMessage mime = new MimeMessage(null, is);
            System.out.println("Subject: " + mime.getSubject());
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      我的问题来自使用 Mockito 模拟 javax.mail.internet.MimeMessage 的构造函数 MimeMessage(Folder, InputStream, int) 所需的 javax.mail.Folder。这将调用javax.mail.Message Message(Folder, int) 的构造函数,然后访问folder.store.session。这导致MimeMessage 的构造函数抛出NullPointerException

      解决方案:

      class ClasspathMimeMessage extends MimeMessage {
          private ClasspathMimeMessage(Folder folder, InputStream is, int msgnum) throws MessagingException {
              super(folder, is, 0);
          }
      
          public static MimeMessage create(String resourceName) {
              Class<PopEmailMmsReceiverTest> loaderClass = PopEmailMmsReceiverTest.class;
              InputStream is = loaderClass.getResourceAsStream(resourceName);
      
              Folder inbox = new MyFolder();
      
              try {
                  return new ClasspathMimeMessage(inbox, is, 0);
              } catch (MessagingException ex) {
                  throw new RuntimeException("Unable to load email from classpath at " + loaderClass.getResource(resourceName).toString());
              }
          }
      }
      
      class MyFolder extends Folder {
          MyFolder() {
              super(createMockStore());
          }
          private static Store createMockStore() {
              return mock(Store.class);
          }
          public void appendMessages(Message[] msgs) throws MessagingException {
          }
          public void close(boolean expunge) throws MessagingException {
          }
          public boolean create(int type) throws MessagingException {
              return false;
          }
          public boolean delete(boolean recurse) throws MessagingException {
              return false;
          }
          public boolean exists() throws MessagingException {
              return false;
          }
          public Message[] expunge() throws MessagingException {
              return null;
          }
          public Folder getFolder(String name) throws MessagingException {
              return null;
          }
          public String getFullName() {
              return null;
          }
          public Message getMessage(int msgnum) throws MessagingException {
              return null;
          }
          public int getMessageCount() throws MessagingException {
              return 0;
          }
          public String getName() {
              return null;
          }
          public Folder getParent() throws MessagingException {
              return null;
          }
          public Flags getPermanentFlags() {
              return null;
          }
          public char getSeparator() throws MessagingException {
              return 0;
          }
          public int getType() throws MessagingException {
              return 0;
          }
          public boolean hasNewMessages() throws MessagingException {
              return false;
          }
          public boolean isOpen() {
              return false;
          }
          public Folder[] list(String pattern) throws MessagingException {
              return null;
          }
          public void open(int mode) throws MessagingException {
          }
          public boolean renameTo(Folder f) throws MessagingException {
              return false;
          }   
      }
      

      这对我来说看起来很丑陋,所以如果有人有更好的建议,我会很高兴听到它。

      【讨论】:

        猜你喜欢
        • 2014-08-01
        • 2019-03-15
        • 1970-01-01
        • 2018-03-08
        • 1970-01-01
        • 2020-03-10
        • 1970-01-01
        • 2021-10-20
        • 2021-06-13
        相关资源
        最近更新 更多