JUnit test case to check that an exception of type javax.mail.MessagingException is not thrown 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?
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.