Domanda

Sto cercando di unit test un metodo che elabora le istanze javax.mail.Message.

Sto scrivendo un convertitore per le email di cambiamento che arrivano in diversi formati e sono quindi trasformati in un formato interno consistente (MyMessage). Questa conversione di solito dipenderà da-indirizzo o rispondere indirizzo della e-mail, e le parti del e-mail, il soggetto, e il da- e Reply-indirizzi saranno necessari per la creazione del nuovo MyMessage.

Ho una collezione di messaggi di posta elettronica prime che vengono salvati localmente come file .eml, e mi piacerebbe fare un test di unità, che carica i file .eml dal percorso di classe e li converte in casi javax.mail.Message. È questo possibile ed in caso affermativo, come sarebbe essere fatto?

È stato utile?

Soluzione 2

Il mio problema è venuto da utilizzare Mockito per deridere il javax.mail.Folder richiesta dal costruttore di javax.mail.internet.MimeMessage MimeMessage(Folder, InputStream, int). Ciò richiede il costruttore per javax.mail.Message Message(Folder, int) che poi accede folder.store.session. Ciò ha provocato un NullPointerException gettati dal costruttore per MimeMessage.

Soluzione:

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

Questo sembra molto brutto per me, quindi se qualcuno ha un suggerimento migliore, sarei felice di sentirlo.

Altri suggerimenti

Dopo alcuni test, ho finalmente caricato con successo un messaggio utilizzando il costruttore MimeMessage(Session, InputStream) pubblico (in contrapposizione a quella protetta basata su cartelle citato nel altra risposta).

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

}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top