How to mock context(ActorContext)?

scala unit-testing testing mocking akka

337 观看


2101 作者的声誉

I am using TestKit to test the Akka Actors. I have a class Demo which has method getActorRef, which takes input as string and returns an ActorRef.

class Demo @Inject()(a: A. b: B, context: ActorContext) {
  def getActorRef(id: String): ActorRef

I have mocked A,B while creating object of Demo.Now i am facing issue how to mock context.

What i did to mock it ? val context = mock[ActorContext]

But it didn't work.

作者: Mahesh Chand 的来源 发布者: 2017 年 12 月 27 日

回应 (1)


10 作者的声誉

It's not clear exactly what you're trying to do, or what errors you're encountering. I'm guessing you're using the ActorContext to get an ActorRef inside getActorRef. If so, have you tried using Mockito to create a mock ActorContext, passing that into Demo when you construct it in the test, then stubbing out the ActorContext method called by getActorRef so that it returns the value you want (i.e., a ref to a test probe, or some such thing)?

I'd expect that to work, assuming that's what you're trying to do.

作者: yashap 发布者: 29.12.2017 08:41