ARYA
ARYA

Reputation: 243

Akka test case assertion failure

I am trying to write test for an actor which sends:

sender() ! Status.Failure(message)

I have written the test case as:

class TestActorSpec
    extends TestKit(ActorSystem("system"))
    with ImplicitSender
    with WordSpecLike
    with Matchers {
  implicit val executionContext: ExecutionContextExecutor = system.dispatcher
  implicit val futureDuration: FiniteDuration             = 60.seconds
  val timeout: FiniteDuration                             = 5.seconds
  val testActorRef: ActorRef                              = system.actorOf(Props(new TestActor()), "test-actor")

  val futureResult: Future[Any] = (testActorRef ? "Exception")(timeout)

  "TestSpec" should {
    "send the exception in {
      expectMsgType[Status.Failure]
    }
  }
}

But i am getting the test failure with reason:

[info] java.lang.AssertionError: assertion failed: timeout (3 seconds) during expectMsgClass waiting for class akka.actor.Status$Failure [info] at scala.Predef$.assert(Predef.scala:223)

Upvotes: 0

Views: 444

Answers (1)

Ivan Stanislavciuc
Ivan Stanislavciuc

Reputation: 7275

If you want to receive Status.Failure as a message, you should not use ask pattern ? and use tell ! instead.

The reason is that ask pattern will consume the Failure and pass back a failed Future. In case of ask pattern, you need to assert a failed Future.

From Scala docs of akka.actor.Status.Failure

This class/message type is preferably used to indicate failure of some operation performed. As an example, it is used to signal failure with AskSupport is used (ask/?).

Upvotes: 2

Related Questions