Scala 在模拟类中使用var时获取空指针异常

Scala 在模拟类中使用var时获取空指针异常,scala,playframework,scalatest,Scala,Playframework,Scalatest,我正在为我的play应用程序编写单元测试。应用程序中的某些数据很少更改,需要经常访问。因此,我在start中获取它并将其存储在hashmap中 var inMemQuestion:Map[Long,QuestionSchema.Question] = { println("Getting all questions from DB.....") Await.result(listAll, Duration.Inf).map(a => a.id->a

我正在为我的play应用程序编写单元测试。应用程序中的某些数据很少更改,需要经常访问。因此,我在start中获取它并将其存储在hashmap中

var inMemQuestion:Map[Long,QuestionSchema.Question] = {
    println("Getting all questions from DB.....")
    Await.result(listAll, Duration.Inf).map(a => a.id->a).toMap
  }
在我的方法中,我使用的是来自这里的数据,而不是数据库

但是,当我试图用这种方法来模拟单元测试时,我的方法是这样的:

val sampleQsn = QuestionSchema.Question(9.toLong,"1 or 2",7.toLong,4.toLong,Some(1.0),Some(false),new java.sql.Timestamp(1599545742),new java.sql.Timestamp(1599552379))
val questionsRepo = mock[QuestionRepository]
when(questionsRepo.inMemQuestion(9)).thenReturn( // null pointer error in this line
  sampleQsn
)

我得到空指针错误。有谁能帮我解决这个问题吗?

因为回购问题被嘲笑了。除非显式提供了模拟实现,否则其所有成员都保留为null。这意味着,
questions.inMemQuestion
null
@sarveshseri是的,我试图为输入9提供实现,并在返回子句时作为sampleQsn输出。
inMemQuestion
不是
方法。它是一个
值成员
@sarveshseri我也尝试给它赋值。。我也有同样的例外。主要关注的是如何解决这个错误,所以如果我尝试在键9处获取值,我会得到
sampleQsn
1。不要使用
var
2。撰写而不是等待