A common pattern when using actors is for actor A to send a message to Actor B and then for Actor B to send a message back.
This can be tested using the Akka testkit along with Scala Test.
Take the following very simple example. We have an Actor called Server which should accept Startup messages and respond with a Ready message when it is ready for clients to send additional messages.
We can start with a noddy implementation that does nothing:
Then write a test to specify the behaviour. Here I've used TestKit along with Scala Test FunSuite.
This test will fail with the following error message:
assertion failed: timeout (3 seconds) during expectMsg while waiting for Ready
As you can probably guess TestKit waited for 3 seconds for a Ready message to be sent back.
To fix the test we add the the following to the Server Actor implementation:
And now the test will pass!
The two important things to take note of are that our test case extended from TestKit, this gives you an ActorSystem. And that the test case mixed in the ImplicitSender trait, this allows us to receive messages use the methods like "expectMsg" to assert that the correct message has been received.