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

Arm*_*and 6 email unit-testing mocking javax.mail mockito

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

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

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

lap*_*apo 11

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

}
Run Code Online (Sandbox Code Playgroud)


Arm*_*and 0

我的问题来自于使用 Mockito 来模拟构造函数javax.mail.Folder所需的内容。这会调用构造函数,然后访问. 这导致的构造函数抛出异常。javax.mail.internet.MimeMessageMimeMessage(Folder, InputStream, int)javax.mail.Message Message(Folder, int)folder.store.sessionNullPointerExceptionMimeMessage

解决方案:

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;
    }   
}
Run Code Online (Sandbox Code Playgroud)

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