Failing a scalatest when akka actor throws exception outside of the test thread

前端 未结 3 964
既然无缘
既然无缘 2021-01-13 06:36

I\'ve had a situation come up and bite me a few times where I\'m testing an Actor and the Actor throws an exception unexpectedly (due to a bug), but the test still passes.

3条回答
  •  清歌不尽
    2021-01-13 07:02

    Other than examining the logs, I can think of two ways to fail tests when an actor crashes:

    • Ensure that no Terminated message is received
    • Check the TestActorRef.isTerminated property

    The latter option is deprecated, so I'll ignore it.

    Watching Other Actors from Probes describes how to setup a TestProbe. In this case it might look something like:

    val probe = TestProbe()
    probe watch ref
    
    // Actual test goes here ...
    
    probe.expectNoMessage()
    

    If the actor dies due to an exception it will generate the Terminated message. If that happens during the test and you expect something else, the test will fail. If it happens after your last message expectation, then the expectNoMessage() should fail when Terminated is received.

提交回复
热议问题