I've been learning Scala recently, and right now, I am trying to do some learning tests with Mockito, but I've been facing something quite odd.
FooService.scala
class FooServie extends Serializable {
val service:FooDaoTrait = FooDao
val fooResult = service.getAlltheFoosFromDatabase
def putTheFoosInASet(): Set[String] = {
FooResult.split(",").toSet
}
}
FooServiceTest.scala
@RunWith(classOf[JUnitRunner])
class FooServiceTest extends FunSuite with MockitoSugar {
val m = mock[FooDaoTrait]
when(m.getAllTheFoosFromDatabase).thenReturn("Hi, my name is foo")
val instance = new FooService{ override val service = m }
test("Let's get da Foos") {
assert(instance.putTheFoosInASet().size === 3) // 3 is a random Value
}
}
So here, when i run the test, Scala IDE is throwing the one and only java.lang.NullPointerException on my class variable FooResult.
But Somehow, when i move FooResult inside the putTheFoosInASet():
def putTheFoosInASet(): Set[String] = {
val FooResult = service.getAlltheFoosFromDatabase
FooResult.split(",").toSet
}
No exception is thrown and everything works well... Why does the previous pattern is throwing me an exception? I am not really sure what can cause this error and calling fooResult on different methods is not really clean imo.
Any opinion will be appreciated. Thanks.
Aucun commentaire:
Enregistrer un commentaire