Skip to content

Instantly share code, notes, and snippets.

@jconwell
Last active January 1, 2016 13:49
Show Gist options
  • Save jconwell/8153746 to your computer and use it in GitHub Desktop.
Save jconwell/8153746 to your computer and use it in GitHub Desktop.
Akka actor that takes another ActorRef as a constructor argument, and forwards all incoming messages to the one passed in. This is useful for unit tests, where you can pass in a JavaTestKit instance to the constructor and all messages passed to this actor will be forwarded to the JavaTestKit instance.
package com.turbo.akka;
import akka.actor.ActorRef;
import akka.actor.UntypedActor;
/**
* Simple actor that takes another actor and forwards all messages to it.
* Useful in unit testing for capturing and testing if a message was received.
* Simply pass in an Akka JavaTestKit probe into the constructor, and all messages
* that are sent to this actor are forwarded to the JavaTestKit probe
*/
public class ForwarderActor extends UntypedActor {
final ActorRef target;
public ForwarderActor(ActorRef target) {
this.target = target;
}
public void onReceive(Object msg) {
target.forward(msg, getContext());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment