将.eml文件加载到javax.mail.Messages中

时间:2010-05-06 14:16:47

标签: unit-testing email mocking mockito javax.mail

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

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

我有一组原始电子邮件,这些电子邮件在本地保存为.eml个文件,我想进行单元测试,从类路径加载.eml文件并将它们转换为{{ 1}}实例。这是可能的,如果是这样,它将如何完成?

2 个答案:

答案 0 :(得分:10)

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

}

答案 1 :(得分:0)

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

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