将.eml文件加载到javax.mail.Messages中 [英] Loading .eml files into javax.mail.Messages
问题描述
我正在尝试单元测试处理 javax.mail.Message
实例的方法。
I'm trying to unit test a method which processes javax.mail.Message
instances.
我我正在编写转换器来更改以不同格式到达的电子邮件,然后将其转换为一致的内部格式( MyMessage
)。此转换通常取决于电子邮件的地址或回复地址,创建新的 MyMessage将需要电子邮件,主题以及从 - 和回复地址的部分。
。
I am writing a converter to change emails which arrive in different formats and are then converted into a consistent internal format (MyMessage
). This conversion will usually depend on the from-address or reply-address of the email, and the parts of the email, the subject, and the from- and reply-addresses will be required for creating the new MyMessage
.
我有一组原始电子邮件,这些电子邮件在本地保存为 .eml
文件,我想进行单元测试,它从类路径加载 .eml
文件,并将它们转换为 javax.mail.Message
实例。这是可能的,如果是这样,怎么办?
I have a collection of raw emails which are saved locally as .eml
files, and I'd like to make a unit test which loads the .eml
files from the classpath and converts them to javax.mail.Message
instances. Is this possible, and if so, how would it be done?
推荐答案
我的问题来自于使用Mockito来嘲笑 javax.mail.Folder
需要 javax.mail.internet.MimeMessage
的构造函数 MimeMessage(文件夹,InputStream,int)
。这调用 javax.mail.Message
消息(Folder,int)
的构造函数,然后访问 folder.store.session
。这导致 MimeMessage
的构造函数抛出 NullPointerException
。
My problem came from using Mockito to mock the javax.mail.Folder
required by javax.mail.internet.MimeMessage
's constructor MimeMessage(Folder, InputStream, int)
. This calls the constructor for javax.mail.Message
Message(Folder, int)
which then accesses folder.store.session
. This resulted in a NullPointerException
being thrown by the constructor for MimeMessage
.
解决方案:
Solution:
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;
}
}
这对我看起来很丑陋,所以如果有人有一个更好的建议,我很高兴听到。
This looks very ugly to me, so if anyone has a better suggestion, I'd be delighted to hear it.
这篇关于将.eml文件加载到javax.mail.Messages中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!