Search code examples
javaunit-testingakkaactor

Unit testing persistent actors in Akka


How do one unit test persistent actors. I understand that there's akka-testkit which one can use to get the underlying actor object of a test actor reference like below.

final TestActorRef<MyActor> ref = TestActorRef.create(system, props, "testA");
final MyActor actor = ref.underlyingActor();
assertTrue(actor.testMe());

But this doesn't work for persistent actors as mentioned here.

Question: how do one unit test a persistent actor methods ?


Solution

  • This question was already answered on the mailing list in this thread. I will expand on the answer given there a bit.

    When testing Actors, TestActorRef should not be overused. Best practice is to test as much as you can by sending to and receiving messages from the actor. Even if using TestActorRef usually you use it to change the state of the actor before testing whatever you want again by sending and receiving messages. Using the direct access to the actor to call methods directly is not really recommended. If you have some more complex logic in your actor that you need to test separately, you might consider putting it into a trait or a different class and testing it without the actor.

    As for testing PersistentActors, a testkit for that is being worked on, you can track the progress here.