I am testing the getDbStatus
method using Mockito in Scala. But I see the following error. Can anyone help me with this?
Error
You have a NullPointerException here:
-> at com.knoldus.services.info.InfoServiceSpec$anon$2.<init>(InfoServiceSpec.scala:50)
because this method call was *not* stubbed correctly:
-> at scala.Option.orElse(Option.scala:447)
daoInstantiator.userDao();
org.mockito.exceptions.verification.SmartNullPointerException:
You have a NullPointerException here:
-> at com.knoldus.services.info.InfoServiceSpec$anon$2.<init>(InfoServiceSpec.scala:50)
because this method call was *not* stubbed correctly:
-> at scala.Option.orElse(Option.scala:447)
daoInstantiator.userDao();
InfoServiceSpec.scala
class InfoServiceSpec extends BaseSpec {
trait Setup {
val daoInstantiator: DaoInstantiator = mock[DaoInstantiator]
val service = new InfoService(conf,daoInstantiator)(ec, logger)
}
"InfoService#getDbStatus" should {
"get DbStatus" in new Setup {
when(daoInstantiator.userDao.count(any[Filter])(any[concurrent.ExecutionContext])).thenReturn(Future(1))
whenReady(service.getDbStatus){ result =>
assert(result == "active")
}
}
}
}
InfoService.scala
class InfoService(config: Config, daoInstantiator: DaoInstantiator)(implicit
val ec: ExecutionContext,
val logger: LoggingAdapter
) {
def getDbStatus: Future[String] = {
var response = Future("inActive")
val users = daoInstantiator.userDao.count(CheckActiveUsers(true))
users.map { _ =>
response = Future.successful("active")
}
Thread.sleep(1000)
response
}
}
DaoInstantiator.scala
class DaoInstantiator(database: MongoDatabase) {
lazy val userDao = new UserDao(database)
}