I'm new to scalamock I had never use it before , I want to test my scala code using mocking but stuck due to error , here is the full detail.
This project basically get and insert the Student data into scyllaDB , for testing I Don't want to hit the actual database so that's why I'm trying to use scalamock
My build.sbt dependency
libraryDependencies ++= "org.scalamock" %% "scalamock" % "5.1.0" % "test"
StudentRepository Class for which i want to write a test.
class StudentRepository @Inject()(studentDB: StudentDB) extends StudentRepositoryTrait {
import studentDB.{session, space}
override def getStudents: Future[List[StudentData]] = {
studentDB.Student.select.all().fetch
}
override def insertStudentData(student: StudentData): Future[ResultSet] = {
studentDB.Student
.store(student)
.future()
}
}
here is my StudentDB Class.
class StudentDB(override val connector: CassandraConnection)
extends Database[StudentDB](connector) {
object Student extends StudenSchema with Connector
}
Here is my TestClass
class RepositoryMockTesting extends AnyFlatSpec with Matchers with MockFactory {
val mockStudentDB: StudentDB = mock[StudentDB]
val studentRepository = new StudentRepository(mockStudentDB)
val studentData: StudentData = StudentData(Some("Zaman Khan"), Some("1"), Some(""), Some(""), None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None)
"StudentRepository" should "get students" in {
val getStudentsResult: Future[List[StudentData]] = studentRepository.getStudents
getStudentsResult.map { studentList =>
studentList should not be empty
}
}
it should "insert student data" in {
val insertStudentsResult: Future[ResultSet] = studentRepository.insertStudentData(studentData)
insertStudentsResult.map { resultSet =>
resultSet.wasApplied() should be(true)
}
}
}
But I'm getting an error
An exception or error caused a run to abort.
java.lang.NullPointerException
at com.outworkers.phantom.database.Database.<init>(Database.scala:35)
at database.studentdb.StudentDB.<init>(StudentData.scala:7)
at mocktest.RepositoryMockTesting$$anon$1.<init>(RepositoryMockTesting.scala:53)
at mocktest.RepositoryMockTesting.<init>(RepositoryMockTesting.scala:53)
Can any one guide me to resolve this error, Any help is appreciated.