Created
July 4, 2012 15:48
-
-
Save rkuhn/3047972 to your computer and use it in GitHub Desktop.
JavaTestKit Sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Copyright (C) 2012 Typesafe Inc. <http://www.typesafe.com> | |
*/ | |
package docs.testkit; | |
import org.junit.AfterClass; | |
import org.junit.Assert; | |
import org.junit.BeforeClass; | |
import org.junit.Test; | |
import akka.actor.ActorRef; | |
import akka.actor.ActorSystem; | |
import akka.actor.Props; | |
import akka.actor.UntypedActor; | |
import akka.testkit.JavaTestKit; | |
import akka.util.Duration; | |
public class TestKitSampleTest { | |
public static class SomeActor extends UntypedActor { | |
ActorRef target = null; | |
public void onReceive(Object msg) { | |
if (msg.equals("hello")) { | |
getSender().tell("world"); | |
if (target != null) | |
target.forward(msg, getContext()); | |
} else if (msg instanceof ActorRef) { | |
target = (ActorRef) msg; | |
getSender().tell("done"); | |
} | |
} | |
} | |
static ActorSystem system; | |
@BeforeClass | |
public static void setup() { | |
system = ActorSystem.create(); | |
} | |
@AfterClass | |
public static void teardown() { | |
system.shutdown(); | |
} | |
@Test | |
public void testIt() { | |
/* | |
* Wrap the whole test procedure within a testkit | |
* initializer if you want to receive actor replies | |
* or use Within(), etc. | |
*/ | |
new JavaTestKit(system) {{ | |
final Props props = new Props(SomeActor.class); | |
final ActorRef subject = system.actorOf(props); | |
// can also use JavaTestKit “from the outside” | |
final JavaTestKit probe = new JavaTestKit(system); | |
// “inject” the probe by passing it to the test | |
// subject like a real resource would be passed | |
// in production | |
subject.tell(probe.getRef(), getRef()); | |
// await the correct response | |
expectMsgEquals(duration("1 second"), "done"); | |
// run() method needs to finish within 3 seconds | |
new Within(duration("3 seconds")) { | |
protected void run() { | |
subject.tell("hello", getRef()); | |
// This is a demo: would normally use | |
// expectMsgEquals(). Wait time is bounded | |
// by 3-second deadline above. | |
new AwaitCond() { | |
protected boolean cond() { | |
return probe.msgAvailable(); | |
} | |
}; | |
// response must have been enqueued to us | |
// before to the probe | |
expectMsgEquals(Duration.Zero(), "world"); | |
// check that the probe we injected earlier | |
// got the msg | |
probe.expectMsgEquals(Duration.Zero(), | |
"hello"); | |
Assert.assertEquals(getRef(), | |
probe.getLastSender()); | |
// Will wait for the rest of the 3 seconds | |
expectNoMsg(); | |
} | |
}; | |
}}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment