Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting instance of class used to create an actor in Akka in test

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.

like image 791
jdevelop Avatar asked Jan 11 '13 13:01

jdevelop


People also ask

How to create an actor instance in Akka?

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.

How do I test with AKKA typed instead of actors?

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:

What do I need to run a test in Akka?

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.

What is an actorsystem in Akka?

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.


1 Answers

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...

like image 81
Randall Schulz Avatar answered Nov 15 '22 05:11

Randall Schulz