8

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

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

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

4

2 回答 2

12

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

}
于 2011-01-10T18:12:19.380 回答
0

我的问题来自使用 Mockito 来模拟javax.mail.Folder所需javax.mail.internet.MimeMessage的构造函数MimeMessage(Folder, InputStream, int)。这将调用构造函数javax.mail.Message Message(Folder, int),然后对其进行访问folder.store.session。这导致NullPointerException构造函数抛出a 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;
    }   
}

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

于 2010-05-06T15:29:51.930 回答