我正在嘗試使用 mockito 在 scala 中運行這個測驗用例,但我看到以下錯誤。誰能向我建議我做錯了什么?
這是我正在運行的測驗用例
測驗用例
class MailerServiceSpec extends BaseSpec {
val http: HttpExt = mock[HttpExt]
val mailService: NotificationService = mock[NotificationService]
val rawJobResponse: JsObject = Json.obj(("aman","verma"))
"Send Emails to multiple/single user" should {
"send email to user" in {
when(http.singleRequest(any[HttpRequest], any[HttpsConnectionContext], any[ConnectionPoolSettings], any[LoggingAdapter])) thenReturn
future(HttpResponse(status = StatusCodes.OK, entity = httpEntity(rawJobResponse)))
whenReady(mailService.sendMessage(any[String],any[String],any[String])(any[ExecutionContext])){ result =>
assert(result == Done)
}
}
}
}
這是我要測驗的 mailerService 的 sendMessage 方法。
NotificationService.scala
override def sendMessage(receivers: List[String], subject: String, content: String)(implicit
ec: ExecutionContext
): Future[Done] = {
val notification = Notification(
recipients = receivers,
cc = Nil,
bcc = Nil,
sender = sender,
htmlBody = content,
htmlTitle = subject
)
val httpHeader = RawHeader("SENDGRID_API_KEY", sendgridApiKey)
(for {
entity <- Marshal(notification).to[MessageEntity]
_ <- makeHttpRequest(
HttpRequest(POST, notificationEmailUrl, entity = entity).addHeader(httpHeader)
)
} yield Done).andThen {
case Failure(exception) => logger.error("Failed to send email", exception)
}
}
錯誤
You have a NullPointerException here:
-> at org.scalatest.concurrent.ScalaFutures$$anon$1.futureValueImpl(ScalaFutures.scala:294)
because this method call was *not* stubbed correctly:
-> at scala.Option.orElse(Option.scala:477)
notificationService.sendMessage(
null,
null,
null,
null
);
uj5u.com熱心網友回復:
你正在呼叫notificationService你的測驗,但它是一個模擬,你沒有定義它的行為。
我認為你不想模擬它,而是構建一個真實的NotificationService,否則你只是在測驗什么。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/489621.html
