jeudi 24 septembre 2015

Mock Javamail not mocking (actually sending to gmail)

I'm having trouble getting mock-javamail to work, although everything seems like it should:

The mock-javamail JAR is in the path (in build.sbt), and I can create a Mailbox but at runtime, my test class actually tries to connect to gmail and send using a real server. My understanding of mock-javamail (from the very, very limited online docs...) is that just putting it in the path should activate mocking.

Here's the test class:

package utility

import java.util._
import javax.mail._
import javax.mail.internet._
import org.jvnet.mock_javamail.Mailbox
import org.specs2.ScalaCheck
import play.api.test.PlaySpecification

class TestMail extends PlaySpecification with ScalaCheck with GPAutomationTestUtilities with GPGenerators with GPTestLogging {
    "the mailer" should {
        "send an email" in {
            val to = new InternetAddress("someone@here.com", "Zac Beckman")
            val from = new InternetAddress("support@here.com", "Here.Com")
            val s = Session.getInstance(new Properties())
            val m = new MimeMessage(s)
            var t: Option[Transport] = None

            try {
                m.setText("Test message.")
                m.setFrom(from)
                m.setRecipient(Message.RecipientType.TO, to)
                m.setSubject("Testing")

                t = Some(s.getTransport("smtps"))

                t.map { transport =>
                    transport.connect("smtp.gmail.com", "nobody", "nopassword")
                    transport.sendMessage(m, m.getAllRecipients)
                }
            } catch {
                case e: Throwable => logger.debug(s"smtp failure: ${e.toString}"); failure
            } finally {
                try {
                    t.map(_.close())
                } catch {
                    case e: Throwable => logger.debug(s"smtp failure: ${e.toString}"); failure
                }
            }

            val inbox = Mailbox.get(to) // Get the mock mailbox server

            inbox.size() == 1 must beTrue // Make sure there is a message waiting
        }
    }
}

Which always fails like so:

[debug] - test - smtp failure: javax.mail.AuthenticationFailedException: 535-5.7.8 Username and Password not accepted. Learn more at
535 5.7.8  http://ift.tt/1b3rDlG o69sm294645ywd.42 - gsmtp
[info] TestMail
[info]
[info] the mailer should
[error]   x send an email
[error]    failure (TestMail.scala:49)

This, despite mock-javamail being in the path (I can create a Mailer, see the last two lines of the test). Here's my dependencies, from build.sbt:

libraryDependencies ++= Seq(
    "com.zaxxer" % "HikariCP" % "2.3.8",
    "com.typesafe.slick" %% "slick" % "3.0.3",
    "com.typesafe.slick" %% "slick-extensions" % "3.0.0",
    "org.joda" % "joda-convert" % "1.7",
    "com.github.tototoshi" %% "slick-joda-mapper" % "2.0.0",
    "org.mindrot" % "jbcrypt" % "0.3m",
    "com.wix" %% "accord-core" % "0.4.2",
    "com.sksamuel.scrimage" %% "scrimage-core" % "2.0.2",
    "org.jvnet.mock-javamail" % "mock-javamail" % "1.9" % "test",
    "com.sun.mail" % "javax.mail" % "1.4.7",
    "com.google.code.findbugs" % "jsr305" % "2.0.3",
    "org.specs2" %% "specs2-scalacheck" % "3.6" % "test",
    specs2 % Test
)

Any ideas what I'm missing? (It feels like I need to take another step to activate the mocking but... according to what I've found online, just adding the JAR to my class path should do the trick).

Aucun commentaire:

Enregistrer un commentaire