Как проверить функцию с помощью mockito? - PullRequest
0 голосов
/ 16 мая 2019

У меня есть функции в классе обслуживания:

  def update(news: News): Future[Int] = {
    newsDAO.update(news)
  }

  def update(id: Long, news: News): Future[Option[News]] = {
    (for {
      oldNews <- OptionT(newsDAO.findOne(id))
      updatedNews = oldNews.update(news)
      _ <- OptionT.liftF(update(updatedNews))
    } yield updatedNews).value
  }

Я пытался протестировать его с помощью mockito, но он не работает. Произошла ошибка во время выполнения функции liftF. Но я не могу понять, почему, Где моя ошибка?

class NewsServiceTest extends AsyncWordSpec with MockitoSugar {
  trait NewsServiceSetup {
    val mockitoNewsDAO: NewsDAO = mock[NewsDAO]

    val newsService = new NewsService(mockitoNewsDAO)
  }

  //update(id: Long, news: News)
  trait Update2Setup extends NewsServiceSetup with FutureTest[Option[News]] {
    when(mockitoNewsDAO.findOne(currentId)).thenReturn(Future(Some(currentNews)))
    when(mockitoNewsDAO.update(updatedCurrentNews)).thenReturn(Future(1))

    override def resultF: Future[Option[News]] = {
      newsService.update(1, updatedCurrentNews)
    }


  "Method update with 2 params" when {
    "id =1 and news = currentNews, function update return updated object News" must {
      "be equals News(title = \"newTitle\", short_title = Some(\"new short title\"), " +
        "text = \"new text\")" in new Update2Setup {}.isExpect(Some(currentNews))
    }
  }
 }

Вот лог с терминала:

[info]   - must be equals News(title = "newTitle", short_title = Some("new short title"), text = "new text") *** FAILED ***
[info]     java.lang.NullPointerException:
[info]     at cats.instances.FutureInstances$$anon$1.map(future.scala:27)
[info]     at cats.instances.FutureInstances$$anon$1.map(future.scala:10)
[info]     at cats.data.OptionT$.liftF(OptionT.scala:200)
[info]     at services.NewsService.$anonfun$update$2(NewsService.scala:51)
[info]     at cats.data.OptionT.$anonfun$flatMap$1(OptionT.scala:43)
[info]     at scala.Option.fold(Option.scala:158)
[info]     at cats.data.OptionT.$anonfun$flatMapF$1(OptionT.scala:46)
[info]     at scala.concurrent.Future.$anonfun$flatMap$1(Future.scala:302)
[info]     at scala.concurrent.impl.Promise.$anonfun$transformWith$1(Promise.scala:37)
[info]     at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:60)

...