javamail-mock2
Open source mock classes for mockup JavaMail (useful especially for unittest). Supports IMAP IDLE.
Features
* Support imap, imaps, pop3, pop3s, smtp, smtps * Support for SMTP: Mock Transport.send() * Supported for POP3: * cast to POP3Folder * Folder.getUID(Message msg) * Supported for IMAP: * cast to IMAPFolder * cast to UIDFolder * IDLE * ID * Subfolders * Folder.getMessagesByUID(...) * delete/rename folders * append messages * Unsupported for the moment: * All IMAP extensions except IDLE and ID * casts to POP3Message/IMAPMessage * store listenersThe library come in two flavors/modes
- Normal (or halfmock): Allows also to connect to real IMAP/POP servers. Use this if you have mixed testing setups (mockend an real server). Require a little bit of setup.
- Fullmock: Use this if you have mocked test only. Normally no setup required.
See unittests for how to use the library. Maven site docu is here: http://salyh.github.io/javamail-mock2/
Usage: Normal (= Halfmock) mode
* Include the javamail-mock2-halfmock-x.x.jar file in your unittest project (or use maven, see below) * Make sure every operation that should be mocked uses as protocol either mock_smtp, mock_imap or mock_pop3 (or mock_smtps, mock_imaps or mock_pop3s) * See unittest how to archive this * Create a mailbox and add folders/messages or use Transport.sendMail to put mails into your INBOX * Use the JavaMail API to retrieve mails via POP3 or IMAP or do whatever your application doesUsage: Fullmock mode
* Include the javamail-mock2-fullmock-x.x.jar file in your unittest project (or use maven, see below) * Create a mailbox and add folders/messages or use Transport.sendMail to put mails into your INBOX * Use the JavaMail API to retrieve mails via POP3 or IMAP or do whatever your application doesMaven: Normal (= Halfmock)
```xml de.saly javamail-mock2-halfmock 0.5-beta4 test ```Maven: Fullmock
```xml de.saly javamail-mock2-fullmock 0.5-beta4 test ```Examples
```javafinal MockMailbox mb = MockMailbox.get("[email protected]");
final MailboxFolder mf = mb.getInbox();
final MimeMessage msg = new MimeMessage((Session) null);
msg.setSubject("Test");
msg.setFrom("[email protected]");
msg.setText("Some text here ...");
msg.setRecipient(RecipientType.TO, new InternetAddress("[email protected]"));
mf.add(msg); // 11
mf.add(msg); // 12
mf.add(msg); // 13
Session session = Session.getInstance(new Properties());
final Store store = session.getStore("pop3s"); //or mock_pop3s for halfmock
store.connect("[email protected]", null);
final Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
Assert.assertEquals(3, inbox.getMessageCount());
Assert.assertNotNull(inbox.getMessage(1));
inbox.close(true);
For a real usage scenario look here: [Elasticsearch IMAP River](https://github.com/salyh/elasticsearch-river-imap)