I have an actor that gets initiated inside the actor, i want to mock the actorB so that the message actorB ? GetDataForProcessing(value) is not sent to actorB and i can test the unit functionality of ActorExp.
My actor is:
class ActorExp(a: ARepo) extends Actor{
lazy val actorB = context.system.actorSelection("user/ActorB")
def receive: Receive = {
case m @ GetData(value) =>
sender() ! getData(value)
}
def getData(value:String) = {
val res = actorB ? GetDataForProcessing(value)
res map {
...
}
}
}
class ActorB(repoB: BRepo) extends Actor{...}
The test that I am trying to write is:
class ActorExpSpecSpec
extends TestKit(ActorSystem("ActorExpSpecSpec"))
with WordSpecLike
with Matchers
with JsonSupport
with MockitoSugar
with BeforeAndAfterAll
with ImplicitSender {
override def afterAll: Unit = TestKit.shutdownActorSystem(system)
implicit val futureDuration: FiniteDuration = 60.seconds
implicit val timeout: Timeout = 10.seconds
val mockedRepoA = mock[ARepo]
val actorA: ActorRef = TestActorRef(ActorExp.props(mockedRepoA))
"ActorExpSpecSpec" should {
"be able to data" in {
val action = (actorA ? GetData("value")).mapTo[Seq[String]]
val result = Await.result(action, 10.seconds)
result should equal("yes")
}
}
}
Now I get ask time out exception as the actorB
is not initialized as it requires certain parameters and I do not want to test actorB
. Is there any way in the above stated scenario that I can mock actorB
and its message actorB ? GetDataForProcessing(value)
?