Testing Actors

The tests in the Hello World example illustrates use of the JUnit framework. The test coverage is not complete. It simply shows how easy it is to test actor code and provides some basic concepts. You could add to it as an exercise to increase your own knowledge.

The test class is using akka.test.javadsl.TestKit, which is a module for integration testing of actors and actor systems. This class only uses a fraction of the functionality provided by TestKit.

Integration testing can help us ensure that Actors are behaving asynchronously. This first test uses TestKit probe to interrogate and verify the expected behavior. Let’s look at a source code snippet:

package com.lightbend.akka.sample;

import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.testkit.javadsl.TestKit;
import com.lightbend.akka.sample.Greeter.*;
import com.lightbend.akka.sample.Printer.*;

import static org.junit.Assert.assertEquals;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

public class AkkaQuickstartTest {
    static ActorSystem system;

    public static void setup() {
        system = ActorSystem.create();

    public static void teardown() {
        system = null;

    public void testGreeterActorSendingOfGreeting() {
        final TestKit testProbe = new TestKit(system);
        final ActorRef helloGreeter = system.actorOf(Greeter.props("Hello", testProbe.getRef()));
        helloGreeter.tell(new WhoToGreet("Akka"), ActorRef.noSender());
        helloGreeter.tell(new Greet(), ActorRef.noSender());
        Greeting greeting = testProbe.expectMsgClass(Greeting.class);
        assertEquals("Hello, Akka", greeting.message);

Once we have a reference to TestKit probe we pass the ActorRef of it to Greeter as part of the constructor arguments. We thereafter send two messages to Greeter; one to set the greeting person to greet and another to trigger the sending of a Greeting. The expectMsg method on the TestKit verifies whether the message got sent.

The example code just scratches the surface of the functionality available in TestKit. A complete overview can be found here.

Now that we’ve reviewed all of the code. Let’s run the example again and look at its output.