我在服务类中有函数:
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)