How do I get instance of class I passed to Props when creating an actor with ActorSystem.actorOf? I need that for unit tests to get reference to some properties of the actor, so the actor is local in the same JVM as test.
I don't want to use Akka's test framework because I need the actor live, it's kind of integration tests.
In Akka you can’t create an instance of an Actor using the new keyword. Instead, you create Actor instances using a factory spawn methods. Spawn does not return an actor instance, but a reference, akka.actor.typed.ActorRef, that points to the actor instance. This level of indirection adds a lot of power and flexibility in a distributed system.
We’ll be using Akka typed instead of the regular classic Actors as recommended by the Akka team. To set up our test suite, we will need to add the test dependencies to our build.sbt: AkkaTestKit is best used with ScalaTest as recommended by the Akka team. AkkaTestKit creates an instance of ActorTestKit which provides access to:
Briefly speaking, you need 3 things to make the test work: an Akka system, the test kit, and the actor under test. Akka system is the environment where both actors (test kit and target actor) are running. Test kit is served as a client who sends the message and asserts the reply later.
An ActorSystem is the intial entry point into Akka. Usually only one ActorSystem is created per application. An ActorSystem has a name and a guardian actor. The bootstrap of your application is typically done within the guardian actor. The guardian actor of this ActorSystem is GreeterMain.
The underlying instance of an Actor subclass is well and truly sealed off from you and you're not going to get at it without mucking with Akka code itself. If you look at the definition of the ActorRef you'll see that it doesn't even contain a reference to the Actor!
Similarly, you cannot instantiate Actor subclasses directly using new
.
I guess the Akka designers were serious about the ActorRef / Actor firewall...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With