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?
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)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With