You should be able to inject the mail session directly into your unit test.
I have a unit test that accesses the mail session through injection. It
looks like this...
public class EmailServiceEJBTest {
final static Logger logger =
LoggerFactory.getLogger(EmailServiceEJBTest.class);
final static String DATABASE_DRIVER = "org.hsqldb.jdbcDriver";
final static String DATABASE_URL = "jdbc:hsqldb:mem:pokersquid";
static SimpleSmtpServer smtpServer;
static EJBContainer ejbContainer;
private static boolean databaseInitialized = false;
@Resource(name="smtpSession")
public Session smtpSession;
@BeforeClass
public static void beforeClass() {
smtpServer = SimpleSmtpServer.start(29876);
Properties p = new Properties();
// SMTP properties
p.put("smtpSession", "new://Resource?type=javax.mail.Session");
p.put("smtpSession.mail.transport.protocol", "smtp");
p.put("smtpSession.mail.smtp.host", "localhost");
p.put("smtpSession.mail.smtp.port", "29876");
p.put("smtpSession.mail.smtp.auth", "false");
ejbContainer = EJBContainer.createEJBContainer(p);
}
@AfterClass
public static void afterClass() {
ejbContainer.close();
smtpServer.stop();
}
@Test
public void testSendSimpleEmail() {
assertNotNull(smtpSession);
assertNotNull(emailService);
emailService.sendMail(
"[email protected]",
"test",
"this is a test email",
"text/plain");
assertEquals(1, smtpServer.getReceivedEmailSize());
Iterator<SmtpMessage> iter = smtpServer.getReceivedEmail();
SmtpMessage email = iter.next();
assertEquals("test", email.getHeaderValue("Subject"));
assertEquals("this is a test email", email.getBody());
}
}
--
View this message in context:
http://openejb.979440.n4.nabble.com/how-to-get-mail-session-from-JNDI-tp4664156p4664205.html
Sent from the OpenEJB User mailing list archive at Nabble.com.