1

There is parent actor which in preStart creates child actors, one for every "active" user. Now I'd like to write a test asserting that it really happens, I mean child actors are really created for preconfigured users.

class IntegrationActor(settingsDao: UserSettingsDAO, childMaker: ActorRefFactory => UserId => ActorRef) extends Actor with StrictLogging {

  val createUserActor = childMaker(context)

  override def preStart(): Unit = {
    val e = settingsDao.findAllEnabled()
    e.map(_.userId).foreach(createUserActor)
  }

  // ... 
}

I have test like this, but it doesn't work as all is happening off-main-test-thread

// mockMaker is already defined/mocked

it should "create child actors for users with enabled integration when started" in {
  // given
  val us = UserSettings(...)
  Mockito.when(settingsDao.findAllEnabled()).thenReturn(Set(us))

  // when
  system.actorOf(Props(new IntegrationActor(settingsDao, mockMaker)))

  // then
  verifyMockMakerCalledFor(us)
}

I was trying to assign CallingThreadDispatcher to parent actor, but with no luck.

Theoretically I could just new this class and test it as usual, but Actor's can't be new-ed by hand.

Is there any other way to do this other than extracting stuff to trait with self type being Actor? Not sure it's the right way.

Michal Ostruszka
  • 2,089
  • 2
  • 20
  • 23

0 Answers0