I have the following:
class Supervisor(dataProvider: DatabaseClientProvider) extends Actor { val writer = context.actorOf(Props(classOf[Child], dataProvider.get)) def receive: Receive = { case Msg => writer forward msg } override val supervisorStrategy = OneForOneStrategy(maxNrOfRetries = 100) { case e: ConnectionException => Resume } } class Child(db: DatabaseClient) extends Actor { def receive: Receive = { case msg:Msg => db.write(text) } }
I want a unit test above the code, basically I'm trying to make sure that when an exception occurs, we will resume processing anyway, as you can see below. The problem is that the supervisor does not rule out an exception. What is the best way to check the code below?
"resume handling messages when exception occurs" in { // Given val msg1 = Msg("Some msg1") val msg2 = Msg("Some msg2") //Throw an exception when attempting to write msg1 val databaseClient = mock[DatabaseClient] when(databaseClient.write(msg1.text).thenThrow(ConnectionException("Error!")) val dataProvider = mock[DatabaseClientProvider] when(dataProvider.get).thenReturn(databaseClient) val supervisor = system.actorOf(Props(new Supervisor(dataProvider))) // When intercept[ConnectionException] { supervisor ! msg1 } // When supervisor ! msg2 // Then verify(databaseClient.write("Some msg"), times(2)) }
source share