Akka FSM: How to write test case to verify state change?

242 Views Asked by At

I am working with Akka FSM. I have implemented state change with AbstractFSMWithStash. But I am not sure how to write a unit test case to verify that the actor changes its state. Does Akka provide any methods to access the state of the actor for unit testing?

I could not find much on the documentation page at https://doc.akka.io/docs/akka/current/fsm.html.

1

There are 1 best solutions below

0
On

With scala, we can use TestFSMRef to create the FSM actor we intend to test and then use stateName. Example:

lazy val saleSystemProxy = TestFSMRef(new SaleSystemProxy, "sales-system-fsm")

"Sale System Proxy" should "start in WAITING state" in {
    saleSystemProxy.stateName shouldBe  ActorState.WAITING
}

it should "stay WAITING if any event passed before configuration" in {
    saleSystemProxy ! contract
    saleSystemProxy.stateName shouldBe ActorState.WAITING
}