1
我已經創建了一個擴展了UnTypedProcessor的actor。我打算用這個演員把它的一些消息保存到磁盤。演員看起來像這樣,我們如何爲akka編寫單元測試UntypedProcessor
public class Shard extends UntypedProcessor {
LoggingAdapter log = Logging.getLogger(getContext().system(), this);
@Override
public void onReceive(Object message) throws Exception {
if(message instanceof CreateTransactionChain) {
System.out.println("Received a CreateTransactionChain message");
ActorRef transactionChain = getContext().actorOf(Props.create(TransactionChain.class), "transactionChain");
Address address = transactionChain.path().address();
getSender().tell(new CreateTransactionReply(address), getSelf());
}
}
}
我有一個像這樣爲這個寫一個單元測試,
public class ShardTest extends AbstractActorTest{
@Test
public void testOnReceiveCreateTransaction() throws Exception {
System.out.println("running tests");
new JavaTestKit(getSystem()) {{
final Props props = Props.create(Shard.class);
final TestActorRef<Shard> subject = TestActorRef.create(getSystem(), props, "test");
// can also use JavaTestKit 「from the outside」
final JavaTestKit probe = new JavaTestKit(getSystem());
// the run() method needs to finish within 3 seconds
new Within(duration("3 seconds")) {
protected void run() {
subject.tell(new CreateTransactionChain(), getRef());
final String out = new ExpectMsg<String>("match hint") {
// do not put code outside this method, will run afterwards
protected String match(Object in) {
if (in instanceof CreateTransactionReply) {
return "match";
} else {
throw noMatch();
}
}
}.get(); // this extracts the received message
assertEquals("match", out);
// Will wait for the rest of the 3 seconds
expectNoMsg();
}
};
}};
}
}
當我運行這個測試UntypeProcessor的方法的onReceive不會被調用。如果我從UntypedActor擴展我的類,而事情工作得很好。任何想法爲什麼擴展UntypedProcessor不起作用?是否需要添加一些配置才能使其發揮作用?有什麼需要嘲笑嗎?