2010-05-06 61 views
5

我试图单元测试一个处理javax.mail.Message实例的方法。将.eml文件加载到javax.mail.Messages中

我正在写一个转换器来更改以不同格式到达的电子邮件,然后转换为一致的内部格式(MyMessage)。此转换通常取决于电子邮件的发件人地址或回复地址,并且创建新的MyMessage需要电子邮件的部分,主题以及发件人和回复地址。

我有一个保存在本地的文件.eml原料电子邮件的集合,我想打从classpath中加载.eml文件,并将其转换为javax.mail.Message实例单元测试。这是否可能,如果是的话,它将如何完成?

回答

0

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

解决方案:

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; 
    } 
} 

这看起来非常难看我,所以如果任何人有更好的建议,我会很高兴听到这个消息。

9

经过几次测试,我终于成功地使用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()); 
    } 

} 
+0

也适用于我 – grasshopper 2015-04-23 15:00:14