我有一个scala-play-slick应用程序,并且正在使用Google guice进行依赖项注入。 光滑版本3.0.3 我的存储库如下所示:
@Singleton
class Repository @Inject()(dbConfigProvider: DatabaseConfigProvider)(
implicit ec: ExecutionContext) {
private val dbConfig = dbConfigProvider.get[JdbcProfile]
import dbConfig._
import profile.api._
private class MyTable(tag: Tag) extends Table[ModelObject](tag, "TableName") {
def id = column[Int]("id", O.PrimaryKey, O.AutoInc)
def * =
(id) <> ((ModelObject.apply _).tupled, ModelObject.unapply)
}
private val myTable = TableQuery[MyTable]
def getAll: Future[Seq[ModelObject]] = db.run {
myTable.result
}
// remaining code
现在,要对代码进行单元测试,我有以下代码
class RepositoryTest extends FunSuite with Matchers with BeforeAndAfterEach {
private var databaseConfigProvider : DatabaseConfigProvider = _
private var repository : Repository = _
override def beforeEach(): Unit = {
databaseConfigProvider = mock(classOf[DatabaseConfigProvider])
repository = new Repository(databaseConfigProvider)
}
test("should get all the model object"){
val result = usersRepository.getAll(userRequest).await
result shouldBe modelObjects
}
}
在运行测试用例时,我得到了Nullpointer异常
java.lang.NullPointerException was thrown.
java.lang.NullPointerException
在getAll方法的db.run
行中。现在我的问题是,我应该如何模拟MyTable以避免空指针异常。
任何指针都将非常有帮助。在此先感谢!!!