I have a method which will send a text mail using JavaMail API and I am trying to write a test case for this method which will check if an Exception of type javax.mail.MessagingException
is not thrown.
If a MessagingException
type exception is thrown, the test will fail, else it will pass.
public void sendMail() throws MessagingException{
final String smtpServer = "smtp.gmail.com";
final String userAccount = ""; // Sender Account.
final String password = ""; // Password/Application Specific Password.
final String SOCKET_FACTORY = "javax.net.ssl.SSLSocketFactory";
final String smtpPort = "587";
final String PORT = "465";
final Properties props = new Properties();
props.put("mail.smpt.host", smtpServer);
props.put("mail.smtp.user", userAccount);
props.put("mail.smtp.password", password);
props.put("mail.smtp.port", smtpPort);
props.put("mail.smtp.auth", true);
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.debug", "false");
props.put("mail.smtp.socketFactory.port", PORT);
props.put("mail.smtp.socketFactory.class", SOCKET_FACTORY);
props.put("mail.smtp.socketFactory.fallback", "false");
Session connection = Session.getInstance(props, null);
MimeMessage mimeMessage = new MimeMessage(connection);
connection.setDebug(true);
final Address toAddress = new InternetAddress(""); // toAddress
final Address fromAddress = new InternetAddress(userAccount);
mimeMessage.setContent("This is a test mail...", "text/html; charset=UTF-8");
mimeMessage.setFrom(fromAddress);
mimeMessage.setRecipient(javax.mail.Message.RecipientType.TO, toAddress);
mimeMessage.setSubject("Test Mail...");
Transport transport = connection.getTransport("smtp");
transport.connect(smtpServer, userAccount, password);
transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
}
public void testSendMailToCheckThatMessagingExceptionIsNotThrown() {
try {
genericTaskInterpreter.sendMail();
} catch (MessagingException messagingException) {
fail("A MessagingException is thrown..." + messagingException.getMessage());
}
}
Am I doing it correctly? How can I improve it?
1 Answer 1
JUnit will fail anyway if an (unexpected) exception is thrown, by creating an Error (instead of a simple failure). As you do not catch any other exceptions (which means every exception fails your test anyway), you can simply remove the try-catch block, as your test case will still fail then if the MessagingException
is thrown.
-
\$\begingroup\$ So, is this a good idea to check that no exceptions are thrown at all?
genericTaskInterpreter.sendMail();thrown.expect(Throwable.class);
\$\endgroup\$Sandeep Chatterjee– Sandeep Chatterjee2015年08月10日 08:35:39 +00:00Commented Aug 10, 2015 at 8:35 -
1\$\begingroup\$ Either expect or don't - there is no try. ;-) I see two choices: Either you expect a specific exception to be thrown or you expect no exception to be thrown. Expecting ANY exception somehow isn't a unit test anymore, it's an experiment. If you don't know what exception your code will throw under which conditions, then the problem is not the unit test, but the code. The assumption that no exceptions are thrown is the default in JUnit, you don't have to do anything for that. Only if you expect a specific exception, use a rule or the expected value of the @Test annotation. \$\endgroup\$Florian Schaetz– Florian Schaetz2015年08月10日 08:43:41 +00:00Commented Aug 10, 2015 at 8:43
-
\$\begingroup\$ And sorry, your line
genericTaskInterpreter.sendMail();thrown.expect(Throwable.class);
doesn't make any sense. You expect that any Throwable was thrown? And you expect so AFTER you actually call the method? \$\endgroup\$Florian Schaetz– Florian Schaetz2015年08月10日 08:44:24 +00:00Commented Aug 10, 2015 at 8:44 -
\$\begingroup\$ Ok. I think I understand your point now. Thanks. \$\endgroup\$Sandeep Chatterjee– Sandeep Chatterjee2015年08月10日 09:01:00 +00:00Commented Aug 10, 2015 at 9:01