Testing AKKA actors with Mockito and FEST-Reflect

01 Jul

Abstract : One of the few frameworks implementing the actor model is AKKA. In fact AKKA is a lot more than just an implementation of the actor model, but within this post we are going to concentrate on combining Mockito, JUnit and FEST-Reflect in order to facilitate actor testing and thus we won’t need all the fancy features of AKKA.
Goal : Mocking an instance field within an AKKA actor.
Acknowledgement: My gratitude goes to the open source community and to the following people:
Munish K Gupta – Using AKKA testkit with Java

In order to reach our goal will need the following maven configuration. Note that you need to add the AKKA repository:

<!-- other maven configuration -->







            <name>Akka Maven Repository</name>
        <!-- other repositories ... -->

The actor which we are going to test looks like this:

import org.springframework.beans.factory.annotation.Autowired;

public class BusinessActor extends UntypedActor {

    private IBusinessService businessService;

    public void onReceive(Object message) {

As you can see, I’m using Spring framework for dependency injection within the actor, but you are free to choose whatever approach you want to instantiate and assign the business service.

Our goal is quite simple, we have to mock the businessService within our BusinessActor. The difficulty comes from the fact that in a test case we are going to have an ActorRef reference variable to our actor and not a plain Actor reference. This means that our actor is actually nested within an ActorRef reference. As you might already guessed this brings further complications for mocking our businessService reference within the actor instance itself. Or in other words, our businessService is deeply nested and the exact path is Luckily for us, since version 1.4 of FEST-Reflect we can play with (deeply) nested fields with a single line of code (not counting the static import). Here is how:

import com.typesafe.config.ConfigFactory;
import org.fest.reflect.core.Reflection;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;

import static org.fest.reflect.core.Reflection.*;
import static org.mockito.Matchers.eq;

public class TestBusinessActor {

    static ActorSystem akkaSystem = ActorSystem.create("honeysoft-test-system", ConfigFactory.load().getConfig("honeysoft-test-system"));

    private IBusinessService businessServiceMock;

    private ActorRef businessActor;

    public void setup() {
        businessActor = akkaSystem.actorOf(new Props(BusinessActor.class));

    public void clean() {

    public void shouldExecuteBusinessMethod() {
        String businessMessage = "Some business message";



        Mockito.verify(businessServiceMock, Mockito.times(1))//

As you can see from the highlighted line 43 setting a deeply nested variable is as simple as specifying the path to it. Note that the exact path is, but the businessActor is already passed to the .in() method and thus as an argument to field() we have only “”.

To run the test you are also going to need a configuration file named application.conf which I have placed in my src/test/resources directory. The content of application.conf is:

honeysoft-test-system {
	akka {
	    mode = test
	  	event-handlers = ["akka.testkit.TestEventListener"]
		loglevel = DEBUG
		actor {
			debug {
			 receive = on
			 autoreceive = on
			 lifecycle = on

Well that was all for this post. Don’t hesitate to leave a comment!


Posted by on July 1, 2012 in AKKA, FEST-Reflect, Java, JUnit, Maven, Mockito


Tags: , , , ,

2 responses to “Testing AKKA actors with Mockito and FEST-Reflect

  1. Jeff

    July 12, 2012 at 17:54

    How did you get the business service autowired into the actor in your real system? Are you having spring call the actorOf method somewhere?

    • Ivan

      July 12, 2012 at 21:03

      Hi Jeff, I don’t know what you mean exactly by “spring call the actorOf”. I have a Spring enabled service which calls the actorOf on an AKKA system with a parameter a custom Props for creating the BusinessActor. In general you don’t need Spring to inject (autowire) an instance in an actor but since Spring is so popular why not use it. Would you be interested in seeing a post about this?


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: