Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocking javax.mail.Transport

Without creating another class that I can inject. Is it possible to mock javax.mail.Transport so I can do some mock testing of the Transport.send() method on Java EE 7?

like image 529
Archimedes Trajano Avatar asked Jul 03 '13 20:07

Archimedes Trajano


1 Answers

Following up on Bill Shanon's solution since Dumbster does not have a currently working Maven Central artifact, I used GreenMail.

Then I used the following code:

final GreenMail mailServer = new GreenMail();
mailServer.start();

final Properties mailSessionProperties = new Properties();
mailSessionProperties.put("mail.smtp.port", String.valueOf(mailServer.getSmtp().getPort()));

final javax.mail.Session mailSession = javax.mail.Session.getInstance(mailSessionProperties);

testObject.setMailSession(mailSession);

That way testObject does not need to change even if it has a static call to Transport.send(message).

like image 182
Archimedes Trajano Avatar answered Nov 02 '22 03:11

Archimedes Trajano