0
\$\begingroup\$

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?

\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
  • \$\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 Aug 10 '15 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 Aug 10 '15 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 Aug 10 '15 at 8:44
  • \$\begingroup\$ Ok. I think I understand your point now. Thanks. \$\endgroup\$ – Sandeep Chatterjee Aug 10 '15 at 9:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.