Skip to content

Instantly share code, notes, and snippets.

@nraychaudhuri
Created April 2, 2015 07:24
Show Gist options
  • Save nraychaudhuri/cb7fd59c529840252dba to your computer and use it in GitHub Desktop.
Save nraychaudhuri/cb7fd59c529840252dba to your computer and use it in GitHub Desktop.
Testing Akka actors using JavaTestKit
import akka.actor.ActorIdentity;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.event.Logging;
import akka.testkit.JavaTestKit;
import akka.testkit.TestProbe;
import com.typesafe.training.coffeehouse.CoffeeHouse;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import scala.concurrent.duration.Duration;
import static org.junit.Assert.*;
@RunWith(JUnit4.class)
public class CoffeeHouseTest {
static ActorSystem system;
@BeforeClass
public static void setup() {
system = ActorSystem.create();
}
@AfterClass
public static void teardown() {
JavaTestKit.shutdownActorSystem(system);
system = null;
}
@Test
public void creatingCoffeeHouseDebug() {
new JavaTestKit(system) {{
new EventFilter<Void>(Logging.Debug.class) {
protected Void run() {
system.actorOf(CoffeeHouse.props());
return null;
}
}.matches(".*[Oo]pen.*").occurrences(1).exec();
}};
}
@Test
public void creatingNewGuestActors() {
new JavaTestKit(system) {{
ActorRef coffeeHouse = system.actorOf(CoffeeHouse.props(), "create-guest");
final String guestPath = "/user/create-guest/$*";
coffeeHouse.tell(CoffeeHouse.CreateGuest.Instance, null);
new AwaitAssert(duration("1 minute"), duration("200 millis")) {
@Override
protected void check() {
system.actorSelection(guestPath)
.tell(new akka.actor.Identify(101), getRef());
ActorIdentity i =
expectMsgClass(duration("100 millis"), ActorIdentity.class);
assertEquals(i.correlationId(), 101);
assertNotNull(i.getRef());
}
};
}};
}
}
import akka.actor.ActorIdentity;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.event.Logging;
import akka.testkit.JavaTestKit;
import akka.testkit.TestProbe;
import com.typesafe.training.coffeehouse.Coffee;
import com.typesafe.training.coffeehouse.CoffeeHouse;
import com.typesafe.training.coffeehouse.Waiter;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import scala.concurrent.duration.Duration;
import static org.junit.Assert.*;
@RunWith(JUnit4.class)
public class WaiterTest {
static ActorSystem system;
@BeforeClass
public static void setup() {
system = ActorSystem.create();
}
@AfterClass
public static void teardown() {
JavaTestKit.shutdownActorSystem(system);
system = null;
}
@Test
public void waiterShouldServeCoffeeOnRequest() {
new JavaTestKit(system) {{
ActorRef waiter = system.actorOf(Waiter.props(), "test-waiter");
waiter.tell(new Waiter.ServeCoffee(new Coffee.Akkaccino()), getRef());
final Waiter.CoffeeServed coffeeServed = expectMsgClass(duration("2 seconds"),
Waiter.CoffeeServed.class);
assertEquals(coffeeServed.coffee,new Coffee.Akkaccino());
}};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment